#2788

Split Strings by Separator

newbie · 220 · lc easy +17 · verified · 76% accepted · 346 likes · top 87%

Description

An array of strings words and a character separator are given. Split every string in words using separator as the delimiter.

Return all non-empty pieces produced by these splits, in the order they appear.

Notes

- separator delimits the split but is not included in any resulting piece.

- A single string may produce more than two pieces.

- The resulting order must match the original order.

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:

Example 2:

"one.two.three" splits into "one", "two", "three"
"four.five" splits into "four", "five"
"six" splits into "six"

Example 3:

Hence, the resulting array is ["one","two","three","four","five","six"].

Example 4:

Input: words = ["$easy$","$problem$"], separator = "$"
Output: ["easy","problem"]
Explanation: In this example we split as follows:

Example 5:

"$easy$" splits into "easy" (excluding empty strings)
"$problem$" splits into "problem" (excluding empty strings)

Example 6:

Hence, the resulting array is ["easy","problem"].

Example 7:

Input: words = ["|||"], separator = "|"
Output: []
Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array [].

Code

1
2
3