Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["one.two.three","four.five","six"] "."
["$easy$","$problem$"] "$"
["|||"] "|"
split-a-string-in-balanced-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2788: Split Strings by Separator
import java.util.regex.Pattern;
class Solution {
public List<String> splitWordsBySeparator(List<String> words, char separator) {
List<String> ans = new ArrayList<>();
for (var w : words) {
for (var s : w.split(Pattern.quote(String.valueOf(separator)))) {
if (s.length() > 0) {
ans.add(s);
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2788: Split Strings by Separator
func splitWordsBySeparator(words []string, separator byte) (ans []string) {
for _, w := range words {
for _, s := range strings.Split(w, string(separator)) {
if s != "" {
ans = append(ans, s)
}
}
}
return
}
# Accepted solution for LeetCode #2788: Split Strings by Separator
class Solution:
def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
return [s for w in words for s in w.split(separator) if s]
// Accepted solution for LeetCode #2788: Split Strings by Separator
/**
* [2788] Split Strings by Separator
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn split_words_by_separator(words: Vec<String>, separator: char) -> Vec<String> {
let mut result = Vec::new();
for s in &words {
for i in s.split(separator) {
let word = String::from(i);
if word.is_empty() {
continue;
}
result.push(word)
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2788() {}
}
// Accepted solution for LeetCode #2788: Split Strings by Separator
function splitWordsBySeparator(words: string[], separator: string): string[] {
return words.flatMap(w => w.split(separator).filter(s => s.length > 0));
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.