Skip to main content
Back to problems
#3597
Medium Algorithms

Partition string

Hash Table String Trie Simulation
58.7% acceptance
Feb 25, 2026
68
7
Given a string s, partition it into unique segments: Start at index 0, extend current segment until it has not been seen before. Once unique, add to list, mark as seen, begin new segment. Return array of segments.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn partition_string(s: String) -> Vec<String> {
    let bytes = s.as_bytes();
    let n = bytes.len();
    let mut seen = std::collections::HashSet::new();
    let mut result = vec![];
    let mut start = 0;

    for end in 0..n {
      let seg = &s[start..=end];
      if !seen.contains(seg) {
        seen.insert(seg.to_string());
        result.push(seg.to_string());
        start = end + 1;
      }
    }

    result
  }
}