Problem

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.

Examples

Example 1

1
2
3
4
5
6
7
8
9
Input: words = ["one.two.three","four.five","six"], separator = "."
Output: ["one","two","three","four","five","six"]
Explanation: In this example we split as follows:

"one.two.three" splits into "one", "two", "three"
"four.five" splits into "four", "five"
"six" splits into "six" 

Hence, the resulting array is ["one","two","three","four","five","six"].

Example 2

1
2
3
4
5
6
7
8
Input: words = ["$easy$","$problem$"], separator = "$"
Output: ["easy","problem"]
Explanation: In this example we split as follows: 

"$easy$" splits into "easy" (excluding empty strings)
"$problem$" splits into "problem" (excluding empty strings)

Hence, the resulting array is ["easy","problem"].

Example 3

1
2
3
Input: words = ["|||"], separator = "|"
Output: []
Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. 

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes)
  • separator is a character from the string ".,|$#@" (excluding the quotes)

Solution

Method 1 – Simple Split and Filter

Intuition

Splitting each string by the separator and removing empty results gives the required output. This works because the split operation naturally handles multiple separators and preserves order.

Approach

  1. Iterate through each string in the input array.
  2. Split the string by the separator character.
  3. Filter out any empty strings from the split result.
  4. Collect all non-empty strings in order.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    vector<string> splitWordsBySeparator(vector<string>& words, char sep) {
        vector<string> ans;
        for (auto& w : words) {
            string cur;
            for (char c : w) {
                if (c == sep) {
                    if (!cur.empty()) ans.push_back(cur);
                    cur.clear();
                } else {
                    cur += c;
                }
            }
            if (!cur.empty()) ans.push_back(cur);
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
func splitWordsBySeparator(words []string, sep rune) []string {
    var ans []string
    for _, w := range words {
        cur := ""
        for _, c := range w {
            if c == sep {
                if len(cur) > 0 {
                    ans = append(ans, cur)
                }
                cur = ""
            } else {
                cur += string(c)
            }
        }
        if len(cur) > 0 {
            ans = append(ans, cur)
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char sep) {
        List<String> ans = new ArrayList<>();
        for (String w : words) {
            StringBuilder cur = new StringBuilder();
            for (char c : w.toCharArray()) {
                if (c == sep) {
                    if (cur.length() > 0) ans.add(cur.toString());
                    cur.setLength(0);
                } else {
                    cur.append(c);
                }
            }
            if (cur.length() > 0) ans.add(cur.toString());
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    fun splitWordsBySeparator(words: List<String>, sep: Char): List<String> {
        val ans = mutableListOf<String>()
        for (w in words) {
            var cur = ""
            for (c in w) {
                if (c == sep) {
                    if (cur.isNotEmpty()) ans.add(cur)
                    cur = ""
                } else {
                    cur += c
                }
            }
            if (cur.isNotEmpty()) ans.add(cur)
        }
        return ans
    }
}
1
2
3
4
5
def splitWordsBySeparator(words: list[str], sep: str) -> list[str]:
    ans = []
    for w in words:
        ans += [x for x in w.split(sep) if x]
    return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn split_words_by_separator(words: Vec<String>, sep: char) -> Vec<String> {
        let mut ans = Vec::new();
        for w in words {
            for part in w.split(sep) {
                if !part.is_empty() {
                    ans.push(part.to_string());
                }
            }
        }
        ans
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    splitWordsBySeparator(words: string[], sep: string): string[] {
        const ans: string[] = [];
        for (const w of words) {
            ans.push(...w.split(sep).filter(x => x.length > 0));
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(N + T) – N is total number of characters, T is total splits.
  • 🧺 Space complexity: O(N) – Output array size is proportional to input size.