Leetcode 2788: Split Strings by Separator

grid47
grid47
Exploring patterns and algorithms
Feb 2, 2024 6 min read

You are given an array ‘words’ of strings and a character ‘separator’. Your task is to split each string in ‘words’ by the separator. After the split, return an array of strings containing the new substrings, excluding any empty strings.
Problem
Approach
Steps
Complexity
Input: You are given an array of strings 'words' and a character 'separator'.
Example: Input: words = ['apple.orange.banana', 'pear.grape', 'peach'], separator = '.'
Constraints:
• 1 <= words.length <= 100
• 1 <= words[i].length <= 20
• characters in words[i] are either lowercase English letters or characters from the string '.,|$#@'.
• separator is a character from the string '.,|$#@'.
Output: Return an array containing the resulting substrings formed after splitting by the separator, excluding empty strings.
Example: Output: ['apple', 'orange', 'banana', 'pear', 'grape', 'peach']
Constraints:
Goal: Split each word in the array by the separator and exclude any empty strings.
Steps:
• Iterate through each word in the 'words' array.
• For each word, split it by the 'separator' character.
• Push non-empty substrings into the result array.
Goal: Constraints for the problem.
Steps:
• 1 <= words.length <= 100
• 1 <= words[i].length <= 20
• separator is from the set of characters '.,|$#@'.
Assumptions:
• Each string in 'words' contains only lowercase English letters or characters from the set '.,|$#@'.
• The separator character is one of the allowed characters.
Input: Input: words = ['apple.orange.banana', 'pear.grape', 'peach'], separator = '.'
Explanation: Split each string by the '.' character, and exclude empty strings. Resulting strings: ['apple', 'orange', 'banana', 'pear', 'grape', 'peach'].

Input: Input: words = ['###'], separator = '#'
Explanation: The string '###' contains only separators, so the result is an empty array [] because no valid substrings are formed.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus