Watch 10 video solutions for Split Strings by Separator, a easy level problem involving Array, String. This walkthrough by Developer Docs has 865 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |