Select K Disjoint Special Substrings

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:

  • Any character present inside the substring should not appear outside it in the string.
  • The substring is not the entire string 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
Inputs = "abcdbaefab", k = 2
Outputtrue
We can select two disjoint special substrings, "cd" and "ef", whose characters do not appear elsewhere in s.
Example 2
Inputs = "cdefdc", k = 3
Outputfalse
There can be at most 2 disjoint special substrings, "e" and "f", so since k = 3, the output is false.

Constraints

  • 2 <= n == s.length <= 5 * 10^4
  • 0 <= k <= 26
  • s consists only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate