Given a string s of length n and an integer k, determine whether it is possible to select k disjoint special substrings.
A special substring is a substring where:
s.Note that all k substrings must be disjoint, meaning they cannot overlap.
Return true if it is possible to select k such disjoint special substrings; otherwise, return false.
Example 1:
Input: s = "abcdbaefab", k = 2
Output: true
Explanation:
"cd" and "ef"."cd" contains the characters 'c' and 'd', which do not appear elsewhere in s."ef" contains the characters 'e' and 'f', which do not appear elsewhere in s.Example 2:
Input: s = "cdefdc", k = 3
Output: false
Explanation:
There can be at most 2 disjoint special substrings: "e" and "f". Since k = 3, the output is false.
Example 3:
Input: s = "abeabe", k = 0
Output: true
Constraints:
2 <= n == s.length <= 5 * 1040 <= k <= 26s consists only of lowercase English letters.Problem Overview: Given a string s and an integer k, select k disjoint substrings such that each substring is special. A substring is special when every character inside it appears only within that substring in the original string. The goal is to determine whether at least k such non‑overlapping substrings can be chosen.
Approach 1: Brute Force Substring Validation (O(n^3) time, O(1) space)
Generate every possible substring using two nested loops. For each substring, verify whether it is special by checking the first and last occurrence of every character in the substring against the boundaries of the substring. If any character appears outside the range, the substring is invalid. Store all valid substrings and try selecting k non‑overlapping ones using another pass or backtracking. This approach demonstrates the definition clearly but becomes impractical because validating every substring requires scanning characters repeatedly.
Approach 2: Character Interval Expansion + Greedy Selection (O(n log n) time, O(n) space)
First compute the first and last occurrence of every character using a hash table. For each index that represents the first appearance of a character, expand an interval starting at that index and extend it to the farthest last occurrence of all characters encountered while scanning. If expansion ever moves before the starting index of any character, the interval cannot form a valid special substring and is discarded. This produces candidate intervals where all character occurrences are contained inside the interval.
Once valid intervals are collected, treat them like a classic non‑overlapping interval scheduling problem. Sort the intervals by ending index using sorting, then greedily pick intervals whose start is greater than the end of the previously chosen interval. Count how many intervals can be selected. If the count is at least k, the answer is true. The key insight is that minimal valid intervals guarantee character isolation, and greedy earliest‑finish selection maximizes the number of disjoint substrings.
Approach 3: Dynamic Programming on Intervals (O(n log n) time, O(n) space)
Instead of greedy selection, you can sort valid intervals and run a dynamic programming solution similar to weighted interval scheduling. Let dp[i] represent the maximum number of valid substrings that can be chosen considering the first i intervals. For each interval, either include it (and add 1 plus the best compatible interval before it) or skip it. Binary search finds the previous non‑overlapping interval efficiently. This approach is useful if the problem variant later adds weights or additional constraints.
Recommended for interviews: Interval expansion followed by greedy interval scheduling. It runs in near linear time, clearly demonstrates understanding of character boundaries, and uses a classic greedy pattern that interviewers expect. The brute force approach helps explain the definition of a special substring, but the interval + greedy solution shows algorithmic maturity.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Substring Validation | O(n^3) | O(1) | Conceptual understanding or very small strings |
| Interval Expansion + Greedy Selection | O(n log n) | O(n) | General optimal solution for selecting maximum non-overlapping special substrings |
| Dynamic Programming on Intervals | O(n log n) | O(n) | Useful if the problem variant introduces weights or additional constraints |
3458. Select K Disjoint Special Substrings | Greedy | Sorting • Aryan Mittal • 2,911 views views
Watch 9 more video solutions →Practice Select K Disjoint Special Substrings with our built-in code editor and test cases.
Practice on FleetCode