Split a String Into the Max Number of Unique Substrings

Medium Topics Company Tags

Split s into nonempty contiguous pieces whose concatenation is s. Every piece must have a different string value. Return the greatest number of pieces in a valid split.

Example 1:

Input: s = "abc"

Output: 3

Explanation: Split into "a", "b", and "c". All three pieces differ, and three characters cannot form more than three nonempty pieces.

Example 2:

Input: s = "aaaa"

Output: 2

Explanation: Split into "a" and "aaa". Three distinct nonempty strings made only of a would need lengths of at least 1, 2, and 3, exceeding the four available characters.

Example 3:

Input: s = "abca"

Output: 3

Explanation: Split into "a", "b", and "ca". Four pieces would each have length 1 and would repeat "a", so three is the maximum.

Constraints:

  • 1 <= s.length <= 16
  • Only lowercase English letters occur.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.