LeetCode #1520 — HARD

Maximum Number of Non-Overlapping Substrings

Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.

Solve on LeetCode
The Problem

Problem Statement

Given a string s of lowercase letters, you need to find the maximum number of non-empty substrings of s that meet the following conditions:

  1. The substrings do not overlap, that is for any two substrings s[i..j] and s[x..y], either j < x or i > y is true.
  2. A substring that contains a certain character c must also contain all occurrences of c.

Find the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length. It can be shown that there exists a unique solution of minimum total length.

Notice that you can return the substrings in any order.

Example 1:

Input: s = "adefaddaccc"
Output: ["e","f","ccc"]
Explanation: The following are all the possible substrings that meet the conditions:
[
  "adefaddaccc"
  "adefadda",
  "ef",
  "e",
  "f",
  "ccc",
]
If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist.

Example 2:

Input: s = "abbaccd"
Output: ["d","bb","cc"]
Explanation: Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length.

Constraints:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.
Patterns Used

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

Problem summary: Given a string s of lowercase letters, you need to find the maximum number of non-empty substrings of s that meet the following conditions: The substrings do not overlap, that is for any two substrings s[i..j] and s[x..y], either j < x or i > y is true. A substring that contains a certain character c must also contain all occurrences of c. Find the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length. It can be shown that there exists a unique solution of minimum total length. Notice that you can return the substrings in any order.

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: Hash Map · Greedy

Example 1

"adefaddaccc"

Example 2

"abbaccd"

Related Problems

  • Maximum Number of Non-overlapping Palindrome Substrings (maximum-number-of-non-overlapping-palindrome-substrings)
Step 02

Core Insight

What unlocks the optimal approach

  • Notice that it's impossible for any two valid substrings to overlap unless one is inside another.
  • We can start by finding the starting and ending index for each character.
  • From these indices, we can form the substrings by expanding each character's range if necessary (if another character exists in the range with smaller/larger starting/ending index).
  • Sort the valid substrings by length and greedily take those with the smallest length, discarding the ones that overlap those we took.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

Source-backed implementations are provided below for direct study and interview prep.

// Accepted solution for LeetCode #1520: Maximum Number of Non-Overlapping Substrings
// Auto-generated Java example from py.
class Solution {
    public void exampleSolution() {
    }
}
// Reference (py):
// # Accepted solution for LeetCode #1520: Maximum Number of Non-Overlapping Substrings
// # Time:  O(n)
// # space: O(1)
// 
// class Solution(object):
//     def maxNumOfSubstrings(self, s):
//         """
//         :type s: str
//         :rtype: List[str]
//         """
//         def find_right_from_left(s, first, last, left):
//             right, i = last[ord(s[left])-ord('a')], left
//             while i <= right:
//                 if first[ord(s[i])-ord('a')] < left:
//                     return -1
//                 right = max(right, last[ord(s[i])-ord('a')])
//                 i += 1
//             return right
//             
//         first, last = [float("inf")]*26, [float("-inf")]*26
//         for i, c in enumerate(s):
//             first[ord(c)-ord('a')] = min(first[ord(c)-ord('a')], i)
//             last[ord(c)-ord('a')] = max(last[ord(c)-ord('a')], i)
//         result = [""]
//         right = float("inf")
//         for left, c in enumerate(s):
//             if left != first[ord(c)-ord('a')]:
//                 continue
//             new_right = find_right_from_left(s, first, last, left)
//             if new_right == -1:
//                 continue
//             if left > right:
//                 result.append("")
//             right = new_right
//             result[-1] = s[left:right+1]
//         return result
// 
// 
// # Time:  O(n)
// # space: O(1)
// class Solution2(object):
//     def maxNumOfSubstrings(self, s):
//         """
//         :type s: str
//         :rtype: List[str]
//         """
//         def find_right_from_left(s, first, last, left):
//             right, i = last[ord(s[left])-ord('a')], left
//             while i <= right:
//                 if first[ord(s[i])-ord('a')] < left:
//                     return -1
//                 right = max(right, last[ord(s[i])-ord('a')])
//                 i += 1
//             return right
// 
//         first, last = [float("inf")]*26, [float("-inf")]*26
//         for i, c in enumerate(s):
//             first[ord(c)-ord('a')] = min(first[ord(c)-ord('a')], i)
//             last[ord(c)-ord('a')] = max(last[ord(c)-ord('a')], i)
//         intervals = []
//         for c in xrange(len(first)):
//             if first[c] == float("inf"):
//                 continue
//             left, right = first[c], find_right_from_left(s, first, last, first[c])
//             if right != -1:
//                 intervals.append((right, left))
//         intervals.sort()  # Time: O(26log26)
//         result, prev = [], -1
//         for right, left in intervals:
//             if left <= prev:
//                 continue
//             result.append(s[left:right+1])
//             prev = right
//         return result
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(n log n)
Space
O(1)

Approach Breakdown

EXHAUSTIVE
O(2ⁿ) time
O(n) space

Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.

GREEDY
O(n log n) time
O(1) space

Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.

Shortcut: Sort + single pass → O(n log n). If no sort needed → O(n). The hard part is proving it works.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

Mutating counts without cleanup

Wrong move: Zero-count keys stay in map and break distinct/count constraints.

Usually fails on: Window/map size checks are consistently off by one.

Fix: Delete keys when count reaches zero.

Using greedy without proof

Wrong move: Locally optimal choices may fail globally.

Usually fails on: Counterexamples appear on crafted input orderings.

Fix: Verify with exchange argument or monotonic objective before committing.