array and hashmap July 24, 2023

Split strings by separator

Time O(n) Space O(n) Open original problem

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.