Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
Given a string s, partition it into unique segments according to the following procedure:
s.Return an array of strings segments, where segments[i] is the ith segment created.
Example 1:
Input: s = "abbccccd"
Output: ["a","b","bc","c","cc","d"]
Explanation:
| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
|---|---|---|---|---|---|
| 0 | "a" | [] | No | "" | ["a"] |
| 1 | "b" | ["a"] | No | "" | ["a", "b"] |
| 2 | "b" | ["a", "b"] | Yes | "b" | ["a", "b"] |
| 3 | "bc" | ["a", "b"] | No | "" | ["a", "b", "bc"] |
| 4 | "c" | ["a", "b", "bc"] | No | "" | ["a", "b", "bc", "c"] |
| 5 | "c" | ["a", "b", "bc", "c"] | Yes | "c" | ["a", "b", "bc", "c"] |
| 6 | "cc" | ["a", "b", "bc", "c"] | No | "" | ["a", "b", "bc", "c", "cc"] |
| 7 | "d" | ["a", "b", "bc", "c", "cc"] | No | "" | ["a", "b", "bc", "c", "cc", "d"] |
Hence, the final output is ["a", "b", "bc", "c", "cc", "d"].
Example 2:
Input: s = "aaaa"
Output: ["a","aa"]
Explanation:
| Index | Segment After Adding | Seen Segments | Current Segment Seen Before? | New Segment | Updated Seen Segments |
|---|---|---|---|---|---|
| 0 | "a" | [] | No | "" | ["a"] |
| 1 | "a" | ["a"] | Yes | "a" | ["a"] |
| 2 | "aa" | ["a"] | No | "" | ["a", "aa"] |
| 3 | "a" | ["a", "aa"] | Yes | "a" | ["a", "aa"] |
Hence, the final output is ["a", "aa"].
Constraints:
1 <= s.length <= 105s contains only lowercase English letters. Problem summary: Given a string s, partition it into unique segments according to the following procedure: Start building a segment beginning at index 0. Continue extending the current segment character by character until the current segment has not been seen before. 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. Repeat until you reach the end of s. Return an array of strings segments, where segments[i] is the ith segment created.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Trie
"abbccccd"
"aaaa"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3597: Partition String
class Solution {
public List<String> partitionString(String s) {
Set<String> vis = new HashSet<>();
List<String> ans = new ArrayList<>();
String t = "";
for (char c : s.toCharArray()) {
t += c;
if (vis.add(t)) {
ans.add(t);
t = "";
}
}
return ans;
}
}
// Accepted solution for LeetCode #3597: Partition String
func partitionString(s string) (ans []string) {
vis := make(map[string]bool)
t := ""
for _, c := range s {
t += string(c)
if !vis[t] {
vis[t] = true
ans = append(ans, t)
t = ""
}
}
return
}
# Accepted solution for LeetCode #3597: Partition String
class Solution:
def partitionString(self, s: str) -> List[str]:
vis = set()
ans = []
t = ""
for c in s:
t += c
if t not in vis:
vis.add(t)
ans.append(t)
t = ""
return ans
// Accepted solution for LeetCode #3597: Partition String
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3597: Partition String
// class Solution {
// public List<String> partitionString(String s) {
// Set<String> vis = new HashSet<>();
// List<String> ans = new ArrayList<>();
// String t = "";
// for (char c : s.toCharArray()) {
// t += c;
// if (vis.add(t)) {
// ans.add(t);
// t = "";
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3597: Partition String
function partitionString(s: string): string[] {
const vis = new Set<string>();
const ans: string[] = [];
let t = '';
for (const c of s) {
t += c;
if (!vis.has(t)) {
vis.add(t);
ans.push(t);
t = '';
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Store all N words in a hash set. Each insert/lookup hashes the entire word of length L, giving O(L) per operation. Prefix queries require checking every stored word against the prefix — O(N × L) per prefix search. Space is O(N × L) for storing all characters.
Each operation (insert, search, prefix) takes O(L) time where L is the word length — one node visited per character. Total space is bounded by the sum of all stored word lengths. Tries win over hash sets when you need prefix matching: O(L) prefix search vs. checking every stored word.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.