Partition String - Problem
Given a string s, partition it into unique segments according to the following procedure:
1. Start building a segment beginning at index 0
2. Continue extending the current segment character by character until the current segment has not been seen before
3. Once the segment is unique, add it to your list of segments, mark it as seen, and begin a new segment from the next index
4. Repeat until you reach the end of s
Return an array of strings segments, where segments[i] is the ith segment created.
Input & Output
Example 1 — Basic Partitioning
$
Input:
s = "abcabc"
›
Output:
["a","b","c","ab","c"]
💡 Note:
Start with "a" (unique), then "b" (unique), then "c" (unique). Next, "a" is seen so extend to "ab" (unique). Finally "c" is seen but we're at end, so add "c".
Example 2 — Repeated Characters
$
Input:
s = "ababccc"
›
Output:
["a","b","ab","c","cc"]
💡 Note:
"a" is unique, "b" is unique, "a" is seen so extend to "ab" (unique), "c" is unique, "c" is seen so extend to "cc" (unique).
Example 3 — Single Character
$
Input:
s = "a"
›
Output:
["a"]
💡 Note:
Only one character, so the entire string "a" forms one unique segment.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
String "abcabc" to be partitioned
2
Process
Build segments ensuring each is unique
3
Output
Array of unique segments ["a","b","c","ab","c"]
Key Takeaway
🎯 Key Insight: Use a hash set to track seen segments for O(1) uniqueness checking while building segments incrementally
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code