Select K Disjoint Special Substrings - Problem

Given a string s of length n and an integer k, determine whether it's possible to select k disjoint special substrings.

A special substring is defined as a substring where:

  • Any character present inside the substring should not appear anywhere else in the string
  • The substring is not the entire string s

All k substrings must be disjoint, meaning they cannot overlap with each other.

Example: In string "abcdef", substring "abc" is special if characters 'a', 'b', 'c' don't appear in the remaining part "def".

Return true if it's possible to select k such disjoint special substrings; otherwise, return false.

Input & Output

example_1.py — Python
$ Input: s = "abcdef", k = 2
Output: true
💡 Note: We can select "abc" (positions 0-2) and "def" (positions 3-5). These are disjoint special substrings since characters in "abc" don't appear in "def" and vice versa.
example_2.py — Python
$ Input: s = "abcabc", k = 1
Output: false
💡 Note: No special substring exists because every character appears multiple times in the string. For example, 'a' appears at positions 0 and 3, so any substring containing 'a' cannot be special.
example_3.py — Python
$ Input: s = "abcd", k = 3
Output: true
💡 Note: We have special substrings like "a", "b", "c", "d", "ab", "cd", etc. We can select 3 disjoint ones: "a" (position 0), "b" (position 1), and "d" (position 3). These don't overlap and each character appears only within its respective substring.

Constraints

  • 1 ≤ s.length ≤ 103
  • 0 ≤ k ≤ s.length
  • s consists of lowercase English letters only
  • Special substrings cannot be the entire string s

Visualization

Tap to expand
Select K Disjoint Special Substrings INPUT String s = "abcdef" a b c d e f 0 1 2 3 4 5 Parameters: n = 6, k = 2 Character Ranges: a: [0,0] b: [1,1] c: [2,2] d: [3,3] e: [4,4] f: [5,5] Each char appears once All are special candidates ALGORITHM STEPS 1 Find Character Ranges Record first/last index of each character in string 2 Build Special Substrings Expand ranges to include all occurrences of chars 3 Greedy Selection Sort by end index, pick non-overlapping substrings 4 Count and Compare Check if count of valid substrings is at least k Greedy Selection: a b c d e f Selected: "a" and "c" Count = 2 (can select more) FINAL RESULT Selected Disjoint Substrings: a and c (example) Why Valid: OK "a" - char only in [0,0] OK "c" - char only in [2,2] OK No overlap between them OK Neither is entire string Output: true Can select k=2 disjoint special substrings Key Insight: The greedy approach works because we want to maximize the number of non-overlapping special substrings. By sorting intervals by their end position and always picking the earliest-ending valid substring, we leave maximum room for subsequent selections, ensuring we find k substrings if possible. TutorialsPoint - Select K Disjoint Special Substrings | Greedy Approach
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
23.8K Views
Medium Frequency
~25 min Avg. Time
847 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