Split a String Into the Max Number of Unique Substrings - Problem

Given a string s, return the maximum number of unique substrings that the given string can be split into.

You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.

A substring is a contiguous sequence of characters within a string.

Input & Output

Example 1 — Basic Case
$ Input: s = "ababccc"
Output: 5
💡 Note: One optimal split: ["a", "b", "ab", "c", "cc"] - all 5 substrings are unique
Example 2 — Short String
$ Input: s = "aba"
Output: 2
💡 Note: Best split: ["ab", "a"] - 2 unique substrings (better than ["a", "ba"] which also gives 2)
Example 3 — All Same Characters
$ Input: s = "aa"
Output: 1
💡 Note: Only one way: ["aa"] - cannot split into ["a", "a"] as both substrings would be identical

Constraints

  • 1 ≤ s.length ≤ 16
  • s contains only lowercase English letters

Visualization

Tap to expand
Split String Into Maximum Unique Substrings"ababccc"Input String↓ Find all possible splits ↓"a""b""ab""c""cc"Optimal Split: 5 unique substringsOutput: 5Maximum number of unique substrings
Understanding the Visualization
1
Input
String s = "ababccc" to be split
2
Process
Find all ways to split into unique substrings
3
Output
Maximum number of unique substrings = 5
Key Takeaway
🎯 Key Insight: Use backtracking to explore all splitting possibilities while maintaining a set of used substrings
Asked in
Google 15 Amazon 12
28.0K Views
Medium Frequency
~25 min Avg. Time
890 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