LeetCode Problem

How to Solve Find Maximum Number of Non Intersecting Substrings

Start by identifying all possible substrings that begin and end with the same character and have at least four letters. Use a state transition dynamic programming approach to track overlapping intervals and select the maximum number of non-intersecting substrings. Hash tables efficiently map character positions, while DP ensures the optimal combination of substrings is chosen without intersections.

GhostInterview Help

Need help with Find Maximum Number of Non Intersecting Substrings without spending extra time grinding it?

GhostInterview can read Find Maximum Number of Non Intersecting Substrings from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3557State transition dynamic programmingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Start by identifying all possible substrings that begin and end with the same character and have at least four letters. Use a state transition dynamic programming approach to track overlapping intervals and select the maximum number of non-intersecting substrings. Hash tables efficiently map character positions, while DP ensures the optimal combination of substrings is chosen without intersections.

Problem Statement

Given a lowercase string, identify all substrings that are at least four characters long and start and end with the same letter. The goal is to maximize the number of such non-overlapping substrings.

Return an integer representing the maximum count of non-intersecting substrings. For example, with word = "abcdeafdef", the output is 2 because "abcdea" and "fdef" are the largest non-overlapping substrings that meet the criteria.

Examples

Example 1

Input: word = "abcdeafdef"

Output: 2

The two substrings are "abcdea" and "fdef" .

Example 2

Input: word = "bcdaaaab"

Output: 1

The only substring is "aaaa" . Note that we cannot also choose "bcdaaaab" since it intersects with the other substring.

Constraints

  • 1 <= word.length <= 2 * 105
  • word consists only of lowercase English letters.

Solution Approach

Precompute Character Intervals

Map each character to its first and last index using a hash table. Expand each interval to include all characters inside to ensure no hidden intersections, preparing for DP selection.

Apply Dynamic Programming

Sort intervals by end index and use DP to decide at each step whether to include the current substring. This ensures the maximum number of non-intersecting substrings is selected while respecting overlaps.

Construct Result

After DP computation, backtrack to collect the selected intervals. Extract the substrings from the original string corresponding to these intervals for the final answer.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n) for precomputing character positions and expanding intervals, plus O(n log n) if intervals are sorted for DP. Space complexity is O(n) for storing intervals and DP tables.

What Interviewers Usually Probe

  • Can you efficiently track all character occurrences for interval construction?
  • Is there a dynamic programming or greedy way to maximize non-overlapping selections?
  • How do you handle hidden overlaps when expanding intervals for each character?

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to expand intervals to cover all characters inside, leading to intersecting substrings.
  • Assuming greedy selection by first appearance always yields the maximum number of substrings.
  • Ignoring the minimum length constraint of four characters when collecting intervals.

Follow-up variants

  • Find maximum non-intersecting substrings without the minimum length restriction.
  • Return the actual substrings instead of the count.
  • Use a different character set, such as uppercase letters or digits, affecting hash table mapping.

How GhostInterview Helps

  • Automatically identifies all valid intervals and prevents overlapping errors in selection.
  • Guides through dynamic programming states to maximize non-intersecting substring count.
  • Visualizes interval expansions and DP decisions for easier debugging and verification.

Topic Pages

FAQ

What is the main pattern used in Find Maximum Number of Non Intersecting Substrings?

This problem follows a state transition dynamic programming pattern combined with hash table preprocessing for character intervals.

Can a substring shorter than four characters be included?

No, only substrings of at least four characters that start and end with the same letter are considered.

How do I handle overlapping substrings efficiently?

Use interval expansion and dynamic programming to track overlaps and ensure maximum non-intersecting substrings.

Is sorting intervals necessary?

Yes, sorting intervals by end index allows efficient DP selection without conflicts.

What data structure helps track first and last occurrences?

A hash table mapping each character to its first and last index simplifies interval construction.

GhostInterview Solver

Need direct help with Find Maximum Number of Non Intersecting Substrings instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find Maximum Number of Non Intersecting Substrings from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.