Skip to content

Commit 62e682f

Browse files
committed
feat: update solutions to lc problem: No.0809
No.0809.Expressive Words
1 parent f55fa10 commit 62e682f

File tree

4 files changed

+63
-66
lines changed

4 files changed

+63
-66
lines changed

solution/0800-0899/0809.Expressive Words/README.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ words = ["hello", "hi", "helo"]
4545

4646
**方法一:双指针**
4747

48-
遍历 `words` 数组,对于每个单词 `t`,判断当前单词 `t` 是否可以通过扩张得到 `s`,如果可以,答案加一
48+
遍历数组 `words`,对于每个单词 $t$,判断当前单词 $t$ 是否可以通过扩张得到 $s$,如果可以,那么答案加一
4949

50-
问题的关键在于判断当前单词 `t` 是否可以通过扩张得到 `s`
50+
因此,问题的关键在于判断单词 $t$ 是否可以通过扩张得到 $s$。这里我们通过一个 $check(s, t)$ 函数来判断。函数的具体实现逻辑如下:
5151

52-
我们可以使用双指针 `i``j` 分别指向 `s``t`,初始时 `i = j = 0`
52+
我们首先用双指针 $i$$j$ 分别指向 $s$$t$,初始时 $i$ 和 $j$ 的值均为 $0$
5353

54-
如果 `i``j` 指向的字符不同,那么 `t` 无法通过扩张得到 `s`,直接返回 `false`;否则,我们需要判断 `s``i` 指向的字符的连续出现次数 `c1``t``j` 指向的字符的连续出现次数 `c2` 的关系。如果 `c1 < c2` 或者 `c1 < 3 && c1 != c2`,那么 `t` 无法通过扩张得到 `s`,直接返回 `false`;否则,将 `i``j` 分别右移 `c1``c2`
54+
如果 $i$$j$ 指向的字符不同,那么 $t$ 无法通过扩张得到 $s$,直接返回 `false`;否则,我们需要判断 $i$ 指向的字符的连续出现次数 $c_1$$j$ 指向的字符的连续出现次数 $c_2$ 的关系。如果 $c_1 \lt c_2$ 或者 $c_1 \lt 3$ 并且 $c_1 \neq c_2$,那么 $t$ 无法通过扩张得到 $s$,直接返回 `false`;否则,将 $i$$j$ 分别右移 $c_1$$c_2$ 次。继续判断
5555

56-
如果 `i``j` 都到达了字符串的末尾,那么 `t` 可以通过扩张得到 `s`,返回 `true`,否则返回 `false`
56+
如果 $i$$j$ 都到达了字符串的末尾,那么 $t$ 可以通过扩张得到 $s$,返回 `true`,否则返回 `false`
5757

58-
时间复杂度 $O(L)$,空间复杂度 $O(1)$。其中 $L$ 为 `words` 数组中所有单词的长度之和
58+
时间复杂度 $O(n \times m + \sum_{i=0}^{m-1} w_i)$,其中 $n$ 和 $m$ 分别为字符串 $s$ 和数组 $words$ 的长度,而 $w_i$ 为数组 $words$ 中第 $i$ 个单词的长度
5959

6060
<!-- tabs:start -->
6161

@@ -143,35 +143,35 @@ class Solution {
143143
class Solution {
144144
public:
145145
int expressiveWords(string s, vector<string>& words) {
146+
auto check = [](string&s, string& t) -> int {
147+
int m = s.size(), n = t.size();
148+
if (n > m) return 0;
149+
int i = 0, j = 0;
150+
while (i < m && j < n) {
151+
if (s[i] != t[j]) return 0;
152+
int k = i;
153+
while (k < m && s[k] == s[i]) ++k;
154+
int c1 = k - i;
155+
i = k, k = j;
156+
while (k < n && t[k] == t[j]) ++k;
157+
int c2 = k - j;
158+
j = k;
159+
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
160+
}
161+
return i == m && j == n;
162+
};
163+
146164
int ans = 0;
147165
for (string& t : words) ans += check(s, t);
148166
return ans;
149167
}
150-
151-
int check(string& s, string& t) {
152-
int m = s.size(), n = t.size();
153-
if (n > m) return 0;
154-
int i = 0, j = 0;
155-
while (i < m && j < n) {
156-
if (s[i] != t[j]) return 0;
157-
int k = i;
158-
while (k < m && s[k] == s[i]) ++k;
159-
int c1 = k - i;
160-
i = k, k = j;
161-
while (k < n && t[k] == t[j]) ++k;
162-
int c2 = k - j;
163-
j = k;
164-
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
165-
}
166-
return i == m && j == n;
167-
}
168168
};
169169
```
170170

171171
### **Go**
172172

173173
```go
174-
func expressiveWords(s string, words []string) int {
174+
func expressiveWords(s string, words []string) (ans int) {
175175
check := func(s, t string) bool {
176176
m, n := len(s), len(t)
177177
if n > m {
@@ -199,7 +199,6 @@ func expressiveWords(s string, words []string) int {
199199
}
200200
return i == m && j == n
201201
}
202-
ans := 0
203202
for _, t := range words {
204203
if check(s, t) {
205204
ans++

solution/0800-0899/0809.Expressive Words/README_EN.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,35 @@ class Solution {
132132
class Solution {
133133
public:
134134
int expressiveWords(string s, vector<string>& words) {
135+
auto check = [](string&s, string& t) -> int {
136+
int m = s.size(), n = t.size();
137+
if (n > m) return 0;
138+
int i = 0, j = 0;
139+
while (i < m && j < n) {
140+
if (s[i] != t[j]) return 0;
141+
int k = i;
142+
while (k < m && s[k] == s[i]) ++k;
143+
int c1 = k - i;
144+
i = k, k = j;
145+
while (k < n && t[k] == t[j]) ++k;
146+
int c2 = k - j;
147+
j = k;
148+
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
149+
}
150+
return i == m && j == n;
151+
};
152+
135153
int ans = 0;
136154
for (string& t : words) ans += check(s, t);
137155
return ans;
138156
}
139-
140-
int check(string& s, string& t) {
141-
int m = s.size(), n = t.size();
142-
if (n > m) return 0;
143-
int i = 0, j = 0;
144-
while (i < m && j < n) {
145-
if (s[i] != t[j]) return 0;
146-
int k = i;
147-
while (k < m && s[k] == s[i]) ++k;
148-
int c1 = k - i;
149-
i = k, k = j;
150-
while (k < n && t[k] == t[j]) ++k;
151-
int c2 = k - j;
152-
j = k;
153-
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
154-
}
155-
return i == m && j == n;
156-
}
157157
};
158158
```
159159

160160
### **Go**
161161

162162
```go
163-
func expressiveWords(s string, words []string) int {
163+
func expressiveWords(s string, words []string) (ans int) {
164164
check := func(s, t string) bool {
165165
m, n := len(s), len(t)
166166
if n > m {
@@ -188,7 +188,6 @@ func expressiveWords(s string, words []string) int {
188188
}
189189
return i == m && j == n
190190
}
191-
ans := 0
192191
for _, t := range words {
193192
if check(s, t) {
194193
ans++
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
class Solution {
22
public:
33
int expressiveWords(string s, vector<string>& words) {
4+
auto check = [](string&s, string& t) -> int {
5+
int m = s.size(), n = t.size();
6+
if (n > m) return 0;
7+
int i = 0, j = 0;
8+
while (i < m && j < n) {
9+
if (s[i] != t[j]) return 0;
10+
int k = i;
11+
while (k < m && s[k] == s[i]) ++k;
12+
int c1 = k - i;
13+
i = k, k = j;
14+
while (k < n && t[k] == t[j]) ++k;
15+
int c2 = k - j;
16+
j = k;
17+
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
18+
}
19+
return i == m && j == n;
20+
};
21+
422
int ans = 0;
523
for (string& t : words) ans += check(s, t);
624
return ans;
725
}
8-
9-
int check(string& s, string& t) {
10-
int m = s.size(), n = t.size();
11-
if (n > m) return 0;
12-
int i = 0, j = 0;
13-
while (i < m && j < n) {
14-
if (s[i] != t[j]) return 0;
15-
int k = i;
16-
while (k < m && s[k] == s[i]) ++k;
17-
int c1 = k - i;
18-
i = k, k = j;
19-
while (k < n && t[k] == t[j]) ++k;
20-
int c2 = k - j;
21-
j = k;
22-
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
23-
}
24-
return i == m && j == n;
25-
}
2626
};

solution/0800-0899/0809.Expressive Words/Solution.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func expressiveWords(s string, words []string) int {
1+
func expressiveWords(s string, words []string) (ans int) {
22
check := func(s, t string) bool {
33
m, n := len(s), len(t)
44
if n > m {
@@ -26,7 +26,6 @@ func expressiveWords(s string, words []string) int {
2626
}
2727
return i == m && j == n
2828
}
29-
ans := 0
3029
for _, t := range words {
3130
if check(s, t) {
3231
ans++

0 commit comments

Comments
 (0)