Skip to content

Commit 31908e8

Browse files
authored
feat: update solutions to lc problem: No.2788 (doocs#2200)
No.2788.Split Strings by Separator
1 parent cafb352 commit 31908e8

File tree

4 files changed

+30
-66
lines changed

4 files changed

+30
-66
lines changed

solution/2700-2799/2788.Split Strings by Separator/README.md

+12-22
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71+
**方法一:模拟**
72+
73+
我们遍历字符串数组 $words$,对于每个字符串 $w$,我们使用 `separator` 作为分隔符进行拆分,如果拆分后的字符串不为空,则将其加入答案数组。
74+
75+
时间复杂度 $O(n \times m)$,空间复杂度 $O(m)$,其中 $n$ 是字符串数组 $words$ 的长度,而 $m$ 是字符串数组 $words$ 中字符串的最大长度。
76+
7177
<!-- tabs:start -->
7278

7379
### **Python3**
@@ -109,25 +115,17 @@ class Solution {
109115
public:
110116
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
111117
vector<string> ans;
112-
for (auto& w : words) {
113-
for (auto& s : split(w, separator)) {
118+
for (const auto& w : words) {
119+
istringstream ss(w);
120+
string s;
121+
while (getline(ss, s, separator)) {
114122
if (!s.empty()) {
115-
ans.emplace_back(s);
123+
ans.push_back(s);
116124
}
117125
}
118126
}
119127
return ans;
120128
}
121-
122-
vector<string> split(string& s, char c) {
123-
vector<string> res;
124-
stringstream ss(s);
125-
string t;
126-
while (getline(ss, t, c)) {
127-
res.push_back(t);
128-
}
129-
return res;
130-
}
131129
};
132130
```
133131
@@ -150,15 +148,7 @@ func splitWordsBySeparator(words []string, separator byte) (ans []string) {
150148

151149
```ts
152150
function splitWordsBySeparator(words: string[], separator: string): string[] {
153-
const ans: string[] = [];
154-
for (const w of words) {
155-
for (const s of w.split(separator)) {
156-
if (s.length > 0) {
157-
ans.push(s);
158-
}
159-
}
160-
}
161-
return ans;
151+
return words.flatMap(w => w.split(separator).filter(s => s.length > 0));
162152
}
163153
```
164154

solution/2700-2799/2788.Split Strings by Separator/README_EN.md

+12-22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Hence, the resulting array is [&quot;easy&quot;,&quot;problem&quot;].
6262

6363
## Solutions
6464

65+
**Solution 1: Simulation**
66+
67+
We traverse the string array $words$. For each string $w$, we use `separator` as the delimiter to split it. If the split string is not empty, we add it to the answer array.
68+
69+
The time complexity is $O(n \times m)$, and the space complexity is $O(m)$, where $n$ is the length of the string array $words$, and $m$ is the maximum length of the strings in the array $words$.
70+
6571
<!-- tabs:start -->
6672

6773
### **Python3**
@@ -99,25 +105,17 @@ class Solution {
99105
public:
100106
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
101107
vector<string> ans;
102-
for (auto& w : words) {
103-
for (auto& s : split(w, separator)) {
108+
for (const auto& w : words) {
109+
istringstream ss(w);
110+
string s;
111+
while (getline(ss, s, separator)) {
104112
if (!s.empty()) {
105-
ans.emplace_back(s);
113+
ans.push_back(s);
106114
}
107115
}
108116
}
109117
return ans;
110118
}
111-
112-
vector<string> split(string& s, char c) {
113-
vector<string> res;
114-
stringstream ss(s);
115-
string t;
116-
while (getline(ss, t, c)) {
117-
res.push_back(t);
118-
}
119-
return res;
120-
}
121119
};
122120
```
123121
@@ -140,15 +138,7 @@ func splitWordsBySeparator(words []string, separator byte) (ans []string) {
140138

141139
```ts
142140
function splitWordsBySeparator(words: string[], separator: string): string[] {
143-
const ans: string[] = [];
144-
for (const w of words) {
145-
for (const s of w.split(separator)) {
146-
if (s.length > 0) {
147-
ans.push(s);
148-
}
149-
}
150-
}
151-
return ans;
141+
return words.flatMap(w => w.split(separator).filter(s => s.length > 0));
152142
}
153143
```
154144

solution/2700-2799/2788.Split Strings by Separator/Solution.cpp

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@ class Solution {
22
public:
33
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
44
vector<string> ans;
5-
for (auto& w : words) {
6-
for (auto& s : split(w, separator)) {
5+
for (const auto& w : words) {
6+
istringstream ss(w);
7+
string s;
8+
while (getline(ss, s, separator)) {
79
if (!s.empty()) {
8-
ans.emplace_back(s);
10+
ans.push_back(s);
911
}
1012
}
1113
}
1214
return ans;
1315
}
14-
15-
vector<string> split(string& s, char c) {
16-
vector<string> res;
17-
stringstream ss(s);
18-
string t;
19-
while (getline(ss, t, c)) {
20-
res.push_back(t);
21-
}
22-
return res;
23-
}
2416
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
function splitWordsBySeparator(words: string[], separator: string): string[] {
2-
const ans: string[] = [];
3-
for (const w of words) {
4-
for (const s of w.split(separator)) {
5-
if (s.length > 0) {
6-
ans.push(s);
7-
}
8-
}
9-
}
10-
return ans;
2+
return words.flatMap(w => w.split(separator).filter(s => s.length > 0));
113
}

0 commit comments

Comments
 (0)