Partition String - Problem

You have a string s that you need to partition into unique segments using a greedy approach. The goal is to create the shortest possible segments while ensuring no two segments are identical.

Algorithm:

  1. Start at index 0 and build a segment character by character
  2. Keep extending the current segment until it becomes unique (not seen before)
  3. Once unique, add it to your result and mark it as "seen"
  4. Start a new segment from the next index
  5. Repeat until the entire string is processed

Example: For string "abcabc", we get segments ["a", "b", "c", "ab", "c"] because when we reach the second 'a', we need "ab" to make it unique since "a" was already seen.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abcabc"
โ€บ Output: ["a", "b", "c", "ab", "c"]
๐Ÿ’ก Note: Start with 'a' (unique) โ†’ 'b' (unique) โ†’ 'c' (unique) โ†’ second 'a' is seen, so extend to 'ab' (unique) โ†’ final 'c' is seen but we're at the end, so it becomes a separate segment
example_2.py โ€” Repeated Pattern
$ Input: s = "aaaa"
โ€บ Output: ["a", "aa", "aaa"]
๐Ÿ’ก Note: First 'a' is unique. Second 'a' is seen, but 'aa' is unique. Third 'a' is seen, 'aa' is seen, but 'aaa' is unique. Fourth 'a' continues the pattern.
example_3.py โ€” All Unique Characters
$ Input: s = "abcdef"
โ€บ Output: ["a", "b", "c", "d", "e", "f"]
๐Ÿ’ก Note: Each character is unique on its own, so each forms its own segment - this is the optimal greedy solution.

Visualization

Tap to expand
Partition String Algorithm VisualizationInput: "abcabc"Step-by-Step Process:Step 1:current = "a", seen = {}, unique โœ“ โ†’ add "a"aStep 2:current = "b", seen = {"a"}, unique โœ“ โ†’ add "b"bStep 3:current = "c", seen = {"a","b"}, unique โœ“ โ†’ add "c"cStep 4:current = "a", seen = {"a","b","c"}, duplicate โœ—Step 5:current = "ab", seen = {"a","b","c"}, unique โœ“ โ†’ add "ab"abStep 6:current = "c", seen = {"a","b","c","ab"}, duplicate but end of stringcResult:["a", "b", "c", "ab", "c"]Hash Set Final State: {"a", "b", "c", "ab"}Time Complexity: O(nยฒ) | Space Complexity: O(n)The greedy approach ensures we always choose the shortest unique segment
Understanding the Visualization
1
Start Building
Begin with the first character and check if it's been seen before
2
Extend or Commit
If unique, save it as a segment. If seen, add another character
3
Track History
Use hash set to remember all previous segments
4
Repeat Process
Continue until the entire string is partitioned
Key Takeaway
๐ŸŽฏ Key Insight: By using a hash set to track seen segments and greedily choosing the shortest unique segment at each step, we can efficiently partition any string while maintaining uniqueness with optimal time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We visit each character once O(n), but string operations and hashing can take O(n) in worst case, giving O(nยฒ) overall

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Hash set stores at most O(n) unique segments, each taking O(n) space in worst case

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists of lowercase English letters only
  • The algorithm must use a greedy approach - always choose the shortest unique segment
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.5K Views
Medium Frequency
~12 min Avg. Time
892 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