Find Maximum Number of Non Intersecting Substrings - Problem

You're given a string word and need to find the maximum number of non-overlapping substrings that satisfy two conditions:

  • Each substring must be at least 4 characters long
  • Each substring must start and end with the same letter

The key challenge is to maximize the count while ensuring no two selected substrings overlap (share any characters).

Example: In the string "abcadefgag", we could select "abca" (positions 0-3) and "gag" (positions 7-9) for a total of 1 valid substring, since "gag" is only 3 characters long.

Think of it as placing non-overlapping brackets around valid substrings to maximize the number of brackets you can place!

Input & Output

example_1.py โ€” Basic Case
$ Input: word = "abcadefgag"
โ€บ Output: 1
๐Ÿ’ก Note: Only one valid substring exists: "abca" (positions 0-3). The substring "gag" is only 3 characters long, so it doesn't meet the minimum length requirement of 4.
example_2.py โ€” Multiple Options
$ Input: word = "aabcaabdaea"
โ€บ Output: 2
๐Ÿ’ก Note: We can select "aabca" (0-4) and "aea" (8-10). Wait, "aea" is only 3 chars, so invalid. Actually, we can take "aabca" (0-4) and "aabda" doesn't work either. The answer is 1 with just "aabca".
example_3.py โ€” No Valid Substrings
$ Input: word = "abcdef"
โ€บ Output: 0
๐Ÿ’ก Note: No character appears at both the start and end of any substring of length โ‰ฅ 4, so no valid substrings exist.

Constraints

  • 1 โ‰ค word.length โ‰ค 2000
  • word consists of lowercase English letters only
  • Each valid substring must be at least 4 characters long
  • Valid substrings must start and end with the same letter

Visualization

Tap to expand
Greedy Selection ProcessString: "abcadefgag"abcadefgag0123456789Step 1: Start at position 0 ('a')"abca" selected!โœ“ Length = 4 (โ‰ฅ4)โœ“ Starts and ends with 'a'Greedy choice: shortest validStep 2: Continue from position 4 ('d')"defgag" - not validโœ— Starts with 'd', ends with 'g'โœ— No matching start/endStep 3: Check remaining characters - no valid substrings"gag"โœ— Length = 3 < 4โœ— Too shortResult: 1 substring selected
Understanding the Visualization
1
Scan Left to Right
Move through the string character by character, looking for opportunities
2
Find Valid Endpoints
When you encounter a character, look ahead to find the earliest matching character at distance โ‰ฅ4
3
Make Greedy Choice
Immediately select the shortest valid substring to leave maximum space for future selections
4
Skip Covered Positions
Jump past the selected substring and continue the process
Key Takeaway
๐ŸŽฏ Key Insight: Greedy selection of shortest valid substrings maximizes the number of non-overlapping selections by preserving the most space for future opportunities.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen