Split Strings by Separator

Easy
539.

Split Strings by Separator

Split Strings by Separator is an easy coding problem on arrays and strings, asked in Amazon and Adobe interviews.

Given an array of strings words and a single-character string separator, split each string in words at every occurrence of separator.

Return all the resulting pieces in their original order, excluding empty pieces. The separators themselves are not part of the result.

Example 1

Input
words = ["one.two.three","four.five","six"], separator = "."
Output
["one","two","three","four","five","six"]

Example 2

Input
words = ["$easy$","$problem$"], separator = "$"
Output
["easy","problem"]
Explanation
The leading and trailing empty pieces are dropped.

Example 3

Input
words = ["|||"], separator = "|"
Output
[]
Explanation
Every piece is empty.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • separator is one of the characters .,|$#@ (excluding quotes).
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} words
 * @param {string} separator
 * @return {string[]}
 */
var splitWordsBySeparator = function(words, separator) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Split Strings by Separator — Solution in Python, Java & C++

Split Strings by Separator solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Split Strings by Separator is an easy coding problem on arrays and strings, asked in Amazon and Adobe interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: Amazon, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an array of strings words and a single-character string separator, split each string in words at every occurrence of separator.

Return all the resulting pieces in their original order, excluding empty pieces. The separators themselves are not part of the result.

Example 1

Input: words = ["one.two.three","four.five","six"], separator = "."
Output: ["one","two","three","four","five","six"]

Example 2

Input: words = ["$easy$","$problem$"], separator = "$"
Output: ["easy","problem"]
Explanation: The leading and trailing empty pieces are dropped.

Example 3

Input: words = ["|||"], separator = "|"
Output: []
Explanation: Every piece is empty.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • separator is one of the characters .,|$#@ (excluding quotes).

Hints for Split Strings by Separator

  1. Hint 1

    Process each word independently and append its pieces to one shared output list.

  2. Hint 2

    Accumulate characters until you hit the separator, then flush.

  3. Hint 3

    Flush only non-empty pieces — and do not forget the final flush after the loop.

How to solve Split Strings by Separator

A manual split keeps the empty-piece rule obvious: build up a buffer, flush it at each separator and at the end, and only emit it when it is non-empty.

Approach

  1. Create the output list.
  2. For each word, walk its characters accumulating into cur.
  3. On a separator, push cur if non-empty and reset it.
  4. After the word ends, push cur once more if non-empty.
  5. Return the collected pieces.

Why it works

Consecutive separators, and separators at either end, all produce empty buffers — which the non-empty guard drops, exactly as the problem requires.

Complexity

  • Time — O(total characters)
  • Space — O(total characters) for the output

Pitfalls

  • Built-in split functions keep empty pieces, so they need a filter afterwards.
  • Several of these separator characters are regex metacharacters, so a regex-based split needs escaping.
  • Forgetting the flush after the final character drops the last piece of every word.

Reference solution

Python

from typing import List

def splitWordsBySeparator(words: List[str], separator: str) -> List[str]:
    out = []
    for w in words:
        for part in w.split(separator):
            if part:
                out.append(part)
    return out

Java

public static String[] splitWordsBySeparator(String[] words, String separator) {
    char sep = separator.charAt(0);
    ArrayList<String> out = new ArrayList<>();
    for (String w : words) {
        StringBuilder cur = new StringBuilder();
        for (int i = 0; i < w.length(); i++) {
            if (w.charAt(i) == sep) {
                if (cur.length() > 0) out.add(cur.toString());
                cur.setLength(0);
            } else {
                cur.append(w.charAt(i));
            }
        }
        if (cur.length() > 0) out.add(cur.toString());
    }
    return out.toArray(new String[0]);
}

C++

vector<string> splitWordsBySeparator(vector<string>& words, string separator) {
    char sep = separator[0];
    vector<string> out;
    for (auto& w : words) {
        string cur;
        for (char c : w) {
            if (c == sep) {
                if (!cur.empty()) out.push_back(cur);
                cur.clear();
            } else {
                cur += c;
            }
        }
        if (!cur.empty()) out.push_back(cur);
    }
    return out;
}

JavaScript

var splitWordsBySeparator = function(words, separator) {
    const out = [];
    for (let i = 0; i < words.length; i++) {
        let cur = "";
        const w = words[i];
        for (let j = 0; j < w.length; j++) {
            if (w.charAt(j) === separator) {
                if (cur.length > 0) out.push(cur);
                cur = "";
            } else {
                cur += w.charAt(j);
            }
        }
        if (cur.length > 0) out.push(cur);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings