Given an array of strings words and a character separator, split each string in words by separator.
Return an array of strings containing the new strings formed after the splits, excluding empty strings.
Notes
separator is used to determine where the split should occur, but it is not included as part of the resulting strings.
Example 1:
Input: words = ["one.two.three","four.five","six"], separator = "." Output: ["one","two","three","four","five","six"] Explanation: 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"] Explanation: In this example we split as follows: "$easy$" splits into "easy" (excluding empty strings) "$problem$" splits into "problem" (excluding empty strings) Hence, the resulting array is ["easy","problem"].
Example 3:
Input: words = ["|||"], separator = "|" Output: [] Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array [].
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 20words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes)separator is a character from the string ".,|$#@" (excluding the quotes)Problem Overview: You receive an array of strings and a separator character. For each string, split it by the separator and collect all non-empty substrings into a single result list. Empty tokens created by consecutive separators must be ignored.
Approach 1: String Split and Filter (O(n) time, O(n) space)
The simplest solution uses the language's built-in split() operation. Iterate through each string in the input array, call split(separator), and append every non-empty token to the result list. The key insight: most languages already implement efficient substring scanning, so you avoid writing the parsing logic yourself. After splitting, filter tokens where token.length == 0 to remove empty segments created by repeated separators like "a,,b". Since every character across all strings is processed once, the total time complexity is O(n), where n is the total number of characters across the array. Space complexity is O(n) for the output list.
This approach is concise and highly readable. It works well in interviews when the language's standard library is allowed. It also aligns naturally with problems involving string manipulation and array traversal.
Approach 2: Manual String Traversal (O(n) time, O(k) space)
Instead of calling split(), parse each string character by character. Maintain a temporary buffer (or start index) to build the current substring. When the separator is encountered, push the accumulated substring to the result only if it is non-empty, then reset the buffer. Continue scanning until the end of the string and append the final segment if it exists. This method gives full control over parsing and avoids allocating intermediate arrays that split() might create.
Every character is visited exactly once, so the time complexity remains O(n). Space complexity is O(k) for the current substring buffer plus the output list. This pattern appears frequently in problems involving manual parsing, tokenization, or streaming input processing.
Recommended for interviews: The built-in split solution is usually the expected answer because it demonstrates clean use of standard libraries and keeps the implementation short. Interviewers may ask for the manual traversal approach as a follow-up to confirm you understand how string parsing works internally. Showing both approaches demonstrates strong fundamentals in string processing and efficient iteration over collections.
This approach involves using the built-in string split functionality that most high-level languages offer. The idea is to iterate over each string in the words array, split them using the given separator, and filter out any empty strings from the results of the split.
This C implementation uses strtok to split each string using the separator. It then filters out empty strings by checking the length of each split part. The results are stored and returned as an array of strings.
Time Complexity: O(n * m), where n is the number of strings and m is the average length of the strings.
Space Complexity: O(n * m), due to the array of strings holding each non-empty split component.
Instead of utilizing built-in split functions, this approach manually traverses each string character by character, identifying substrings between occurrences of the separator and directly constructing the result list. It can offer additional flexibility or optimizations in environments where built-in functions are inefficient.
This implementation involves manually traversing the string and collecting characters into a buffer until the separator is encountered, at which point the collected segment is added to the results if it is not empty.
Time Complexity: O(n * m) due to manual traversal of all characters.
Space Complexity: O(n * m) enough to store all possible resulting strings.
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.
The time complexity is O(n times m), and the space complexity is O(m), where n is the length of the string array words, and m is the maximum length of the strings in the array words.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: String Split and Filter | Time Complexity: O(n * m), where n is the number of strings and m is the average length of the strings. |
| Approach 2: Manual String Traversal | Time Complexity: O(n * m) due to manual traversal of all characters. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Split and Filter | O(n) | O(n) | Best general solution when standard library functions like split() are allowed |
| Manual String Traversal | O(n) | O(k) | Useful when implementing custom parsers or avoiding intermediate arrays |
Leetcode | 2788. Split Strings by Separator | Easy | Java Solution • Developer Docs • 865 views views
Watch 9 more video solutions →Practice Split Strings by Separator with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor