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
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
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code