Split Strings by Separator - Problem
Split Strings by Separator

Imagine you have a collection of text documents that need to be broken down into individual words or phrases. Given an array of words and a specific separator character, your task is to split each string in the array by the separator and create a new array containing all the resulting substrings.

Key Requirements:
• Split each string wherever the separator appears
Exclude empty strings from the result
• The separator itself should not appear in the output
• Maintain the original order of strings and their splits
• A single string may produce multiple splits

Example: If you have ["one.two.three", "four.five"] with separator '.', you should return ["one", "two", "three", "four", "five"].

Input & Output

example_1.py — Basic Split
$ Input: words = ["one.two.three", "four.five", "six"], separator = '.'
Output: ["one", "two", "three", "four", "five", "six"]
💡 Note: The first string splits into 3 parts, second into 2 parts, and third remains as is since it contains no separator.
example_2.py — Empty Strings Filtered
$ Input: words = ["$easy$", "$$$"], separator = '$'
Output: ["easy"]
💡 Note: The first string has empty strings before and after 'easy', and the second string produces only empty strings. All empty strings are filtered out.
example_3.py — No Separators
$ Input: words = ["hello", "world"], separator = ','
Output: ["hello", "world"]
💡 Note: Since neither string contains the separator, they are returned unchanged in the result array.

Constraints

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 20
  • separator is a single character
  • words[i] consists of lowercase English letters and separator only

Visualization

Tap to expand
Document Processing PipelineDocument 1"one.two.three"Document 2"four.five""one""two""three""four""five"Processing Rules1. Split at separator character ('.')2. Separator not included in result3. Empty strings are filtered out4. Order is preservedExample: "a..b" → ["a", "b"] (not ["a", "", "b"])Final Result Array"one""two""three""four""five"Time Complexity: O(n*m) | Space Complexity: O(k)n = number of strings, m = average string length, k = number of result strings
Understanding the Visualization
1
Load Documents
Start with an array of text documents containing separator characters
2
Process Each Document
Apply splitting logic to break each document at separator positions
3
Filter Results
Remove any empty chunks that result from consecutive separators
4
Collect Output
Combine all valid text chunks into the final result array
Key Takeaway
🎯 Key Insight: Built-in string split functions are highly optimized for this type of text processing, automatically handling edge cases like consecutive separators while maintaining excellent performance.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 5
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen