Skip to main content

Split Strings by Separator - Solution & Explanation

EasyArrayString16 min readAsked at: Amazon, Apple, Coupang
Practice this problem

Problem Statement

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.
  • A split may result in more than two strings.
  • The resulting strings must maintain the same order as they were initially given.

 

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 <= 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)

Approach Overview

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 1: Approach 1: String Split and Filter

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Approach 2: Manual String Traversal

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m) due to manual traversal of all characters.
Space Complexity: O(n * m) enough to store all possible resulting strings.

Try this approach in the editor →

Approach 3: 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.

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(n * m), due to the array of strings holding each non-empty split component.

Approach 2: Manual String Traversal

Time Complexity: O(n * m) due to manual traversal of all characters.
Space Complexity: O(n * m) enough to store all possible resulting strings.

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Split and FilterO(n)O(n)Best general solution when standard library functions like split() are allowed
Manual String TraversalO(n)O(k)Useful when implementing custom parsers or avoiding intermediate arrays

Video Solution

Leetcode | 2788. Split Strings by Separator | Easy | Java SolutionDeveloper Docs937 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Split Strings by Separator easy or hard?
Split Strings by Separator is classified as an Easy problem. The main requirement is understanding basic string operations and filtering empty substrings after splitting.
Split Strings by Separator Python/Java solution
In Python, iterate through the words list, call word.split(separator), and append non-empty strings to the result list. In Java, use String.split with a loop to filter empty tokens. Both implementations run in O(n) time.
How to solve Split Strings by Separator in O(n)?
Iterate through each string and either call split(separator) or manually scan characters while building substrings. Each character is processed exactly once, and non-empty segments are appended to the result list. This guarantees linear time complexity O(n).
What is the best approach for Split Strings by Separator?
The most practical approach uses the built-in split() function for each string and filters out empty tokens. This processes all characters once and keeps the implementation concise. The overall time complexity is O(n), where n is the total number of characters across all strings.
Is Split Strings by Separator asked at Google/Amazon/Meta?
String parsing and tokenization problems frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the pattern of splitting strings and filtering tokens is a common interview exercise.
What data structure is used in Split Strings by Separator?
The main structure is a dynamic list or array that stores the resulting substrings. During manual traversal, a temporary string buffer or index pointer is used to build segments before adding them to the result.
What is the time complexity of Split Strings by Separator?
The optimal solution runs in O(n) time because each character in the input array is examined once during splitting or traversal. Space complexity is O(n) for storing the resulting substrings.

Ready to solve this problem?

Practice Split Strings by Separator with our built-in code editor and test cases.

Practice on FleetCode