LeetCode Problem Workspace

Split Strings by Separator

Split Strings by Separator requires iterating through an array of strings and splitting each by a given separator character efficiently.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Array plus String

bolt

Answer-first summary

Split Strings by Separator requires iterating through an array of strings and splitting each by a given separator character efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus String

Try AiBox Copilotarrow_forward

This problem is about taking an array of strings and a separator character, then splitting each string by that separator. Empty strings resulting from consecutive separators must be excluded from the output. Efficient iteration and string handling ensure correct results even when strings contain multiple separators or unusual characters.

Problem Statement

You are given an array of strings called words and a character separator. For each string in words, split it at every occurrence of the separator and collect all non-empty parts into a new array. The order of the strings in the result should reflect the original order and split order.

Return the array of resulting strings after performing all splits. Ignore any empty strings that arise from leading, trailing, or consecutive separators. You must handle each string individually and combine the results in order.

Examples

Example 1

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

Output: ["one","two","three","four","five","six"]

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

Input: words = ["$easy$","$problem$"], separator = "$"

Output: ["easy","problem"]

In this example we split as follows:

"easyeasy" splits into "easy" (excluding empty strings) "problemproblem" splits into "problem" (excluding empty strings)

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

Example 3

Input: words = ["|||"], separator = "|"

Output: []

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 Approach

Iterate and Split Each String

Loop through each string in words and apply the built-in split method using the separator. Filter out empty strings immediately after splitting to avoid adding invalid entries.

Flatten the Results

Collect the split results from all strings into a single array. Maintain the original sequence by concatenating results from each string in order.

Return Cleaned Array

After iterating and flattening, ensure the final array contains only non-empty strings. This step avoids returning entries created by consecutive separators or edge cases.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n * m) where n is the number of strings and m is the average string length, due to iterating and splitting each string. Space complexity is O(total number of resulting substrings) as we store all non-empty parts.

What Interviewers Usually Probe

  • Look for handling consecutive separators correctly.
  • Ensure empty strings are not included in the result.
  • Check if the solution maintains the original order of words and splits.

Common Pitfalls or Variants

Common pitfalls

  • Including empty strings after splitting at consecutive separators.
  • Not preserving the order of strings after flattening all splits.
  • Using complex nested loops instead of simple iteration and split.

Follow-up variants

  • Split strings by multiple different separator characters instead of one.
  • Return only substrings of a certain minimum length after splitting.
  • Split a single long string multiple times using an array of separators.

FAQ

What happens if a string has multiple consecutive separators?

Consecutive separators produce empty strings when split, which should be filtered out so they are not included in the final array.

Can the separator appear at the start or end of a string?

Yes, but any resulting empty substrings from leading or trailing separators must be ignored in the output.

Is the order of strings important in the output?

Yes, the resulting array should maintain the original word order and the sequence of splits within each word.

How does this relate to the Array plus String pattern?

This problem combines array iteration with string manipulation, a hallmark of the Array plus String pattern, focusing on split operations and result aggregation.

What if the array contains only separators?

If all strings only contain separators, splitting them results in empty strings, so the final returned array will be empty.

terminal

Solution

Solution 1: Simulation

We traverse the string array $words$. For each string $w$, we use `separator` as the delimiter to split it. If the split string is not empty, we add it to the answer array.

1
2
3
class Solution:
    def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
        return [s for w in words for s in w.split(separator) if s]
Split Strings by Separator Solution: Array plus String | LeetCode #2788 Easy