Split Strings by Separator

easy string simulation

Problem

You are given an array of strings words and a character separator. Split each string in words by separator and return a single array of all the resulting pieces, in order, excluding any empty strings.

Inputwords = ["one.two.three","four.five"], separator = "."
Output["one","two","three","four","five"]
Each word is split on '.' and the empty pieces (none here) are dropped.

def split_words_by_separator(words, separator):
    result = []
    for w in words:
        for part in w.split(separator):
            if part != "":
                result.append(part)
    return result
function splitWordsBySeparator(words, separator) {
  const result = [];
  for (const w of words) {
    for (const part of w.split(separator)) {
      if (part !== "") {
        result.push(part);
      }
    }
  }
  return result;
}
class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> result = new ArrayList<>();
        for (String w : words) {
            for (String part : w.split(java.util.regex.Pattern.quote(String.valueOf(separator)))) {
                if (!part.isEmpty()) {
                    result.add(part);
                }
            }
        }
        return result;
    }
}
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
    vector<string> result;
    for (const string& w : words) {
        string cur = "";
        for (char c : w) {
            if (c == separator) {
                if (!cur.empty()) result.push_back(cur);
                cur = "";
            } else cur += c;
        }
        if (!cur.empty()) result.push_back(cur);
    }
    return result;
}
Time: O(total characters) Space: O(total characters)