leetcode
leetcode 2351 ~ 2400
按分隔符拆分字符串

按分隔符拆分字符串

难度:

标签:

题目描述

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.

 

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 <= 100
  • 1 <= words[i].length <= 20
  • characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes)
  • separator is a character from the string ".,|$#@" (excluding the quotes)

代码结果

运行时间: 19 ms, 内存: 16.1 MB


/*
 * 思路:
 * 1. 使用 Java Stream 流操作遍历字符串数组 words。
 * 2. 对每个字符串使用 split 方法进行拆分。
 * 3. 使用 flatMap 展平拆分后的字符串数组。
 * 4. 使用 filter 去除空字符串。
 * 5. 使用 collect 将结果收集到 List 中。
 */

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Solution {
    public List<String> splitWordsStream(String[] words, String separator) {
        return Arrays.stream(words)
                .flatMap(word -> Arrays.stream(word.split(separator)))
                .filter(splitWord -> !splitWord.isEmpty())
                .collect(Collectors.toList());
    }
}

解释

方法:

题解采用了列表推导式来处理问题,通过两层循环来实现。首先,外层循环遍历输入的字符串数组 `words`,内层循环则是对每个字符串使用 `split` 方法按照 `separator` 进行拆分。`split` 方法会返回一个列表,其中包含分割后的子字符串。如果分割后的子字符串非空(即 `if s` 部分),则将其包含在最终的结果列表中。这种方法直接利用 Python 的内建字符串处理功能,简洁且高效。

时间复杂度:

O(L)

空间复杂度:

O(L)

代码细节讲解

🦆
在此题解中,列表推导式的使用是否会影响代码的可读性,尤其是当涉及两层循环时?
列表推导式确实能提高代码的简洁性和执行效率,但同时它也可能影响代码的可读性,特别是当涉及到嵌套循环的情况。对于不熟悉列表推导式的编程者来说,理解其中的逻辑可能需要花费更多的时间。在此题解中使用的双层列表推导式,虽然简化了代码量,但对于初学者来说,可能需要仔细分析才能理解其功能。建议在实际使用时根据团队的编码习惯和成员的熟练程度来决定是否使用复杂的列表推导式。
🦆
此算法如何处理包含连续分隔符的情况,例如 'one..two.three',是否会产生空字符串,它们是如何被处理的?
在解析题解的算法中,如果遇到连续分隔符(如 'one..two.three' 中的两个连续点),使用 `split` 方法将会在结果中产生空字符串。然而,题解中使用的列表推导式包括了一个过滤器 `if s`,这个过滤器确保只有非空的子字符串被添加到最终的结果列表中。因此,即使输入字符串中包含连续的分隔符,产生的空字符串也会被自动过滤掉,不会出现在输出结果中。
🦆
如果输入数组 `words` 中的元素为空字符串,例如 ['', 'one.two'], 那么这种情况下的输出是什么?
如果输入数组 `words` 中包含空字符串,如例子中的 ['', 'one.two'],算法将如下处理这些情况:对于空字符串的元素,使用 `split` 方法也会返回一个包含一个空字符串的列表。由于列表推导式中包含 `if s` 条件,这个空字符串将不会被添加到最终的输出列表中。对于 'one.two',它被正常分割为 ['one', 'two']。因此,对于输入 ['', 'one.two'],最终的输出将是 ['one', 'two']。

相关问题