LeetCode 2788.按分隔符拆分字符串:模拟(字符串处理)

【LetMeFly】2788.按分隔符拆分字符串:模拟(字符串处理)

力扣题目链接:https://leetcode.cn/problems/split-strings-by-separator/

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

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

注意

  • separator 用于决定拆分发生的位置,但它不包含在结果字符串中。
  • 拆分可能形成两个以上的字符串。
  • 结果字符串必须保持初始相同的先后顺序。

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

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • words[i] 中的字符要么是小写英文字母,要么就是字符串 ".,|$#@" 中的字符(不包括引号)
  • separator 是字符串 ".,|$#@" 中的某个字符(不包括引号)

方法一:模拟

新建一个空的字符串数组作为答案,遍历字符串数组的所有字符串,使用一个变量last记录上一个separator的位置(初始值为-1)。

接着遍历这个字符串的每一个字符,如果遍历到了字符串尾或当前字符为separator,就看当前下标和last之间是否有字符存在。若有,则添加到答案数组中。

最终返回答案数组即为答案。

  • 时间复杂度 O ( s u m c h a r a c t e r ( w o r d s ) ) O(sum_character(words)) O(sumcharacter(words)),时间复杂度为字符串数组中字符串的个数
  • 空间复杂度 O ( 1 ) O(1) O(1),力扣的算法返回值不计入算法的空间复杂的

AC代码

C++
cpp 复制代码
class Solution {
public:
    vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
        vector<string> ans;
        for (string& word : words) {
            int last = -1;
            for (int i = 0; i <= word.size(); i++) {
                if (i == word.size() || word[i] == separator) {
                    if (i - last > 1) {
                        ans.push_back(word.substr(last + 1, i - last - 1));
                    }
                    last = i;
                }
            }
        }
        return ans;
    }
};
Python
python 复制代码
# from typing import List

class Solution:
    def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
        ans = []
        for word in words:
            splited = word.split(separator)
            for this in splited:  # 过滤空串
                if this:
                    ans.append(this)
        return ans

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/135724019

相关推荐
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
元亓亓亓1 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
仙俊红1 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
_不会dp不改名_2 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌2 天前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun2 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌2 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家2 天前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌2 天前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学2 天前
【贪心算法】day10
c++·算法·leetcode·贪心算法