|
| 1 | +--- |
| 2 | +title: 2788.按分隔符拆分字符串 |
| 3 | +date: 2024-01-20 23:18:08 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 字符串, 字符串解析, 模拟, 遍历] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】2788.按分隔符拆分字符串:模拟(字符串处理) |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/split-strings-by-separator/](https://leetcode.cn/problems/split-strings-by-separator/) |
| 10 | + |
| 11 | +<p>给你一个字符串数组 <code>words</code> 和一个字符 <code>separator</code> ,请你按 <code>separator</code> 拆分 <code>words</code> 中的每个字符串。</p> |
| 12 | + |
| 13 | +<p>返回一个由拆分后的新字符串组成的字符串数组,<strong>不包括空字符串</strong> 。</p> |
| 14 | + |
| 15 | +<p><strong>注意</strong></p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li><code>separator</code> 用于决定拆分发生的位置,但它不包含在结果字符串中。</li> |
| 19 | + <li>拆分可能形成两个以上的字符串。</li> |
| 20 | + <li>结果字符串必须保持初始相同的先后顺序。</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | + |
| 25 | +<p><strong>示例 1:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>输入:</strong>words = ["one.two.three","four.five","six"], separator = "." |
| 29 | +<strong>输出:</strong>["one","two","three","four","five","six"] |
| 30 | +<strong>解释:</strong>在本示例中,我们进行下述拆分: |
| 31 | + |
| 32 | +"one.two.three" 拆分为 "one", "two", "three" |
| 33 | +"four.five" 拆分为 "four", "five" |
| 34 | +"six" 拆分为 "six" |
| 35 | + |
| 36 | +因此,结果数组为 ["one","two","three","four","five","six"] 。</pre> |
| 37 | + |
| 38 | +<p><strong>示例 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>输入:</strong>words = ["$easy$","$problem$"], separator = "$" |
| 42 | +<strong>输出:</strong>["easy","problem"] |
| 43 | +<strong>解释:</strong>在本示例中,我们进行下述拆分: |
| 44 | + |
| 45 | +"$easy$" 拆分为 "easy"(不包括空字符串) |
| 46 | +"$problem$" 拆分为 "problem"(不包括空字符串) |
| 47 | + |
| 48 | +因此,结果数组为 ["easy","problem"] 。 |
| 49 | +</pre> |
| 50 | + |
| 51 | +<p><strong>示例 3:</strong></p> |
| 52 | + |
| 53 | +<pre> |
| 54 | +<strong>输入:</strong>words = ["|||"], separator = "|" |
| 55 | +<strong>输出:</strong>[] |
| 56 | +<strong>解释:</strong>在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 </pre> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | + |
| 60 | +<p><strong>提示:</strong></p> |
| 61 | + |
| 62 | +<ul> |
| 63 | + <li><code>1 <= words.length <= 100</code></li> |
| 64 | + <li><code>1 <= words[i].length <= 20</code></li> |
| 65 | + <li><code>words[i]</code> 中的字符要么是小写英文字母,要么就是字符串 <code>".,|$#@"</code> 中的字符(不包括引号)</li> |
| 66 | + <li><code>separator</code> 是字符串 <code>".,|$#@"</code> 中的某个字符(不包括引号)</li> |
| 67 | +</ul> |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## 方法一:模拟 |
| 72 | + |
| 73 | +新建一个空的字符串数组作为答案,遍历字符串数组的所有字符串,使用一个变量```last```记录上一个```separator```的位置(初始值为```-1```)。 |
| 74 | + |
| 75 | +接着遍历这个字符串的每一个字符,如果遍历到了字符串尾或当前字符为```separator```,就看当前下标和```last```之间是否有字符存在。若有,则添加到答案数组中。 |
| 76 | + |
| 77 | +最终返回答案数组即为答案。 |
| 78 | + |
| 79 | ++ 时间复杂度$O(sum_character(words))$,时间复杂度为字符串数组中字符串的个数 |
| 80 | ++ 空间复杂度$O(1)$,力扣的算法返回值不计入算法的空间复杂的 |
| 81 | + |
| 82 | +### AC代码 |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +public: |
| 89 | + vector<string> splitWordsBySeparator(vector<string>& words, char separator) { |
| 90 | + vector<string> ans; |
| 91 | + for (string& word : words) { |
| 92 | + int last = -1; |
| 93 | + for (int i = 0; i <= word.size(); i++) { |
| 94 | + if (i == word.size() || word[i] == separator) { |
| 95 | + if (i - last > 1) { |
| 96 | + ans.push_back(word.substr(last + 1, i - last - 1)); |
| 97 | + } |
| 98 | + last = i; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return ans; |
| 103 | + } |
| 104 | +}; |
| 105 | +``` |
| 106 | +
|
| 107 | +#### Python |
| 108 | +
|
| 109 | +```python |
| 110 | +# from typing import List |
| 111 | +
|
| 112 | +class Solution: |
| 113 | + def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: |
| 114 | + ans = [] |
| 115 | + for word in words: |
| 116 | + splited = word.split(separator) |
| 117 | + for this in splited: # 过滤空串 |
| 118 | + if this: |
| 119 | + ans.append(this) |
| 120 | + return ans |
| 121 | +``` |
| 122 | + |
| 123 | +> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/)哦~ |
| 124 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/135724019](https://letmefly.blog.csdn.net/article/details/135724019) |
0 commit comments