Skip to main content
Back to problems
#2788
Easy Algorithms

Split strings by separator

Array String
76.0% acceptance
Feb 25, 2026
346
13
Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes separator is used to determine where the split should occur, but it is not included as part of the resulting strings. A split may result in more than two strings. The resulting strings must maintain the same order as they were initially given.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn split_words_by_separator(words: Vec<String>, separator: char) -> Vec<String> {
    let mut result = vec![];
    for word in &words {
      for part in word.split(separator) {
        if !part.is_empty() {
          result.push(part.to_string());
        }
      }
    }
    result
  }
}