Skip to content

Commit 67d2f81

Browse files
committed
feat: add solutions to lc problem: No.0809
No.0809.Expressive Words
1 parent a593b75 commit 67d2f81

File tree

6 files changed

+410
-2
lines changed

6 files changed

+410
-2
lines changed

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

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,170 @@ words = ["hello", "hi", "helo"]
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:双指针**
47+
48+
遍历 `words` 数组,对于每个单词 `t`,判断当前单词 `t` 是否可以通过扩张得到 `s`,如果可以,答案加一。
49+
50+
问题的关键在于判断当前单词 `t` 是否可以通过扩张得到 `s`
51+
52+
我们可以使用双指针 `i``j` 分别指向 `s``t`,初始时 `i = j = 0`
53+
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` 次。
55+
56+
如果 `i``j` 都到达了字符串的末尾,那么 `t` 可以通过扩张得到 `s`,返回 `true`,否则返回 `false`
57+
58+
时间复杂度 $O(L)$,空间复杂度 $O(1)$。其中 $L$ 为 `words` 数组中所有单词的长度之和。
59+
4660
<!-- tabs:start -->
4761

4862
### **Python3**
4963

5064
<!-- 这里可写当前语言的特殊实现逻辑 -->
5165

5266
```python
53-
67+
class Solution:
68+
def expressiveWords(self, s: str, words: List[str]) -> int:
69+
def check(s, t):
70+
m, n = len(s), len(t)
71+
if n > m:
72+
return False
73+
i = j = 0
74+
while i < m and j < n:
75+
if s[i] != t[j]:
76+
return False
77+
k = i
78+
while k < m and s[k] == s[i]:
79+
k += 1
80+
c1 = k - i
81+
i, k = k, j
82+
while k < n and t[k] == t[j]:
83+
k += 1
84+
c2 = k - j
85+
j = k
86+
if c1 < c2 or (c1 < 3 and c1 != c2):
87+
return False
88+
return i == m and j == n
89+
90+
return sum(check(s, t) for t in words)
5491
```
5592

5693
### **Java**
5794

5895
<!-- 这里可写当前语言的特殊实现逻辑 -->
5996

6097
```java
98+
class Solution {
99+
public int expressiveWords(String s, String[] words) {
100+
int ans = 0;
101+
for (String t : words) {
102+
if (check(s, t)) {
103+
++ans;
104+
}
105+
}
106+
return ans;
107+
}
108+
109+
private boolean check(String s, String t) {
110+
int m = s.length(), n = t.length();
111+
if (n > m) {
112+
return false;
113+
}
114+
int i = 0, j = 0;
115+
while (i < m && j < n) {
116+
if (s.charAt(i) != t.charAt(j)) {
117+
return false;
118+
}
119+
int k = i;
120+
while (k < m && s.charAt(k) == s.charAt(i)) {
121+
++k;
122+
}
123+
int c1 = k - i;
124+
i = k;
125+
k = j;
126+
while (k < n && t.charAt(k) == t.charAt(j)) {
127+
++k;
128+
}
129+
int c2 = k - j;
130+
j = k;
131+
if (c1 < c2 || (c1 < 3 && c1 != c2)) {
132+
return false;
133+
}
134+
}
135+
return i == m && j == n;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int expressiveWords(string s, vector<string>& words) {
146+
int ans = 0;
147+
for (string& t : words) ans += check(s, t);
148+
return ans;
149+
}
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+
}
168+
};
169+
```
61170

171+
### **Go**
172+
173+
```go
174+
func expressiveWords(s string, words []string) int {
175+
check := func(s, t string) bool {
176+
m, n := len(s), len(t)
177+
if n > m {
178+
return false
179+
}
180+
i, j := 0, 0
181+
for i < m && j < n {
182+
if s[i] != t[j] {
183+
return false
184+
}
185+
k := i
186+
for k < m && s[k] == s[i] {
187+
k++
188+
}
189+
c1 := k - i
190+
i, k = k, j
191+
for k < n && t[k] == t[j] {
192+
k++
193+
}
194+
c2 := k - j
195+
j = k
196+
if c1 < c2 || (c1 != c2 && c1 < 3) {
197+
return false
198+
}
199+
}
200+
return i == m && j == n
201+
}
202+
ans := 0
203+
for _, t := range words {
204+
if check(s, t) {
205+
ans++
206+
}
207+
}
208+
return ans
209+
}
62210
```
63211

64212
### **...**

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

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,147 @@ We can&#39;t extend &quot;helo&quot; to get &quot;heeellooo&quot; because the gr
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def expressiveWords(self, s: str, words: List[str]) -> int:
60+
def check(s, t):
61+
m, n = len(s), len(t)
62+
if n > m:
63+
return False
64+
i = j = 0
65+
while i < m and j < n:
66+
if s[i] != t[j]:
67+
return False
68+
k = i
69+
while k < m and s[k] == s[i]:
70+
k += 1
71+
c1 = k - i
72+
i, k = k, j
73+
while k < n and t[k] == t[j]:
74+
k += 1
75+
c2 = k - j
76+
j = k
77+
if c1 < c2 or (c1 < 3 and c1 != c2):
78+
return False
79+
return i == m and j == n
80+
81+
return sum(check(s, t) for t in words)
5982
```
6083

6184
### **Java**
6285

6386
```java
87+
class Solution {
88+
public int expressiveWords(String s, String[] words) {
89+
int ans = 0;
90+
for (String t : words) {
91+
if (check(s, t)) {
92+
++ans;
93+
}
94+
}
95+
return ans;
96+
}
97+
98+
private boolean check(String s, String t) {
99+
int m = s.length(), n = t.length();
100+
if (n > m) {
101+
return false;
102+
}
103+
int i = 0, j = 0;
104+
while (i < m && j < n) {
105+
if (s.charAt(i) != t.charAt(j)) {
106+
return false;
107+
}
108+
int k = i;
109+
while (k < m && s.charAt(k) == s.charAt(i)) {
110+
++k;
111+
}
112+
int c1 = k - i;
113+
i = k;
114+
k = j;
115+
while (k < n && t.charAt(k) == t.charAt(j)) {
116+
++k;
117+
}
118+
int c2 = k - j;
119+
j = k;
120+
if (c1 < c2 || (c1 < 3 && c1 != c2)) {
121+
return false;
122+
}
123+
}
124+
return i == m && j == n;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int expressiveWords(string s, vector<string>& words) {
135+
int ans = 0;
136+
for (string& t : words) ans += check(s, t);
137+
return ans;
138+
}
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+
}
157+
};
158+
```
64159

160+
### **Go**
161+
162+
```go
163+
func expressiveWords(s string, words []string) int {
164+
check := func(s, t string) bool {
165+
m, n := len(s), len(t)
166+
if n > m {
167+
return false
168+
}
169+
i, j := 0, 0
170+
for i < m && j < n {
171+
if s[i] != t[j] {
172+
return false
173+
}
174+
k := i
175+
for k < m && s[k] == s[i] {
176+
k++
177+
}
178+
c1 := k - i
179+
i, k = k, j
180+
for k < n && t[k] == t[j] {
181+
k++
182+
}
183+
c2 := k - j
184+
j = k
185+
if c1 < c2 || (c1 != c2 && c1 < 3) {
186+
return false
187+
}
188+
}
189+
return i == m && j == n
190+
}
191+
ans := 0
192+
for _, t := range words {
193+
if check(s, t) {
194+
ans++
195+
}
196+
}
197+
return ans
198+
}
65199
```
66200

67201
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int expressiveWords(string s, vector<string>& words) {
4+
int ans = 0;
5+
for (string& t : words) ans += check(s, t);
6+
return ans;
7+
}
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+
}
26+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
func expressiveWords(s string, words []string) int {
2+
check := func(s, t string) bool {
3+
m, n := len(s), len(t)
4+
if n > m {
5+
return false
6+
}
7+
i, j := 0, 0
8+
for i < m && j < n {
9+
if s[i] != t[j] {
10+
return false
11+
}
12+
k := i
13+
for k < m && s[k] == s[i] {
14+
k++
15+
}
16+
c1 := k - i
17+
i, k = k, j
18+
for k < n && t[k] == t[j] {
19+
k++
20+
}
21+
c2 := k - j
22+
j = k
23+
if c1 < c2 || (c1 != c2 && c1 < 3) {
24+
return false
25+
}
26+
}
27+
return i == m && j == n
28+
}
29+
ans := 0
30+
for _, t := range words {
31+
if check(s, t) {
32+
ans++
33+
}
34+
}
35+
return ans
36+
}

0 commit comments

Comments
 (0)