Split strings by separator

July 24, 2023

array-and-hashmap

Problem URL: Split strings by separator

We will iterate over the words and split them by the separator. We will then append the non-empty words to the result list.

class Solution:
    def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
        res = []
        for word in words:
            splits = word.split(separator)
            res.extend(filter(lambda x: x, splits))
        return res

Time Complexity: O(n) where n is the number of words.
Space Complexity: O(n) where n is the number of words.