列表

详情


6921. 按分隔符拆分字符串

给你一个字符串数组 words 和一个字符 separator ,请你按 separator 拆分 words 中的每个字符串。

返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串

注意

 

示例 1:

输入:words = ["one.two.three","four.five","six"], separator = "."
输出:["one","two","three","four","five","six"]
解释:在本示例中,我们进行下述拆分:

"one.two.three" 拆分为 "one", "two", "three"
"four.five" 拆分为 "four", "five"
"six" 拆分为 "six" 

因此,结果数组为 ["one","two","three","four","five","six"] 。

示例 2:

输入:words = ["$easy$","$problem$"], separator = "$"
输出:["easy","problem"]
解释:在本示例中,我们进行下述拆分:

"$easy$" 拆分为 "easy"(不包括空字符串)
"$problem$" 拆分为 "problem"(不包括空字符串)

因此,结果数组为 ["easy","problem"] 。

示例 3:

输入:words = ["|||"], separator = "|"
输出:[]
解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: vector<string> splitWordsBySeparator(vector<string>& words, char separator) { } };

cpp 解法, 执行用时: 23 ms, 内存消耗: 43.9 MB, 提交时间: 2024-01-20 15:09:54

class Solution {
public:
    vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
        vector<string> res;
        for (string &word : words) {
            stringstream ss(word);
            string sub;
            while (getline(ss, sub, separator)) {
                if (!sub.empty()) {
                    res.push_back(sub);
                }
            }
        }
        return res;
    }
};

java 解法, 执行用时: 6 ms, 内存消耗: 43.3 MB, 提交时间: 2023-07-24 09:20:54

class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        String sep = "\\" + String.valueOf(separator);
        List<String> list = new ArrayList<>();
        for (String i : words){
            String[] sp = i.split(sep);
            for (String j : sp){
                if (j != null && j.length() >= 1) list.add(j); //空的不加
            }
        }
        return list;
    }
}

python3 解法, 执行用时: 56 ms, 内存消耗: 16.1 MB, 提交时间: 2023-07-24 09:19:35

class Solution:
    def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
        return list(filter(str.__len__, chain.from_iterable(map(partial(str.split, sep=separator), words))))

javascript 解法, 执行用时: 108 ms, 内存消耗: 45.9 MB, 提交时间: 2023-07-24 09:18:43

/**
 * @param {string[]} words
 * @param {character} separator
 * @return {string[]}
 */
   var splitWordsBySeparator = function(words, separator) {
       let res = [];
       for (let i = 0; i < words.length; i++) {
           let sp = words[i].split(separator);
           for (let j = 0; j < sp.length; j++) {
               if (sp[j] != "") res.push(sp[j]);
           }
       }
       return res;
   };

golang 解法, 执行用时: 20 ms, 内存消耗: 7 MB, 提交时间: 2023-07-24 09:17:50

func splitWordsBySeparator(words []string, separator byte) []string {
    ans := []string{}
    for _, word := range words {
        arr := strings.Split(word, string(separator))
        for _, w := range arr {
            if w != "" {
                ans = append(ans, w)
            }
        }
    }
    return ans
}

python3 解法, 执行用时: 68 ms, 内存消耗: 16.1 MB, 提交时间: 2023-07-24 09:13:20

class Solution:
    def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
        ans = []
        for word in words:
            ans.extend([w for w in word.split(separator) if w])
        return ans

上一题