Skip to content

Commit 6f55238

Browse files
committed
feat: add solutions to lc problem: No.0408
No.0408.Valid Word Abbreviation
1 parent 52dd693 commit 6f55238

File tree

7 files changed

+345
-3
lines changed

7 files changed

+345
-3
lines changed

solution/0200-0299/0277.Find the Celebrity/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ ans not knows 6
9898
ans = 6
9999
```
100100
101-
$ans$ 认识 $3$,说明 $ans$ 不是名人($0$ 不是名人),那么名人会是 $1$ 或者 $2$ 吗?不会!因为若 $1$ 或者 $2$ 是名人,那么 $0$ 应该认识 $1$ 或者 $2$ 才对,与前面的例子冲突。因此,我们可以直接将 $ans$ 更新为 $i$。
101+
这里 $ans$ 认识 $3$,说明 $ans$ 不是名人($0$ 不是名人),那么名人会是 $1$ 或者 $2$ 吗?不会!因为若 $1$ 或者 $2$ 是名人,那么 $0$ 应该认识 $1$ 或者 $2$ 才对,与前面的例子冲突。因此,我们可以直接将 $ans$ 更新为 $i$。
102102
103103
我们找出 $ans$ 之后,接下来再遍历一遍,判断 $ans$ 是否满足名人的条件。若不满足,返回 $-1$。
104104

solution/0400-0499/0408.Valid Word Abbreviation/README.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,144 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:模拟**
66+
67+
模拟字符匹配替换。
68+
69+
同时遍历 $word$ 和 $abbr$,若 $abbr$ 遇到数字,则 $word$ 跳过对应数字长度的字符数。若数字为空,或者有前导零,则提前返回 false。
70+
71+
时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。其中 $m$ 是 $word$ 的长度,$n$ 是 $abbr$ 的长度。
72+
6573
<!-- tabs:start -->
6674

6775
### **Python3**
6876

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

7179
```python
72-
80+
class Solution:
81+
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
82+
i = j = 0
83+
m, n = len(word), len(abbr)
84+
while i < m:
85+
if j >= n:
86+
return False
87+
if word[i] == abbr[j]:
88+
i, j = i + 1, j + 1
89+
continue
90+
k = j
91+
while k < n and abbr[k].isdigit():
92+
k += 1
93+
t = abbr[j: k]
94+
if not t.isdigit() or t[0] == '0' or int(t) == 0:
95+
return False
96+
i += int(t)
97+
j = k
98+
return i == m and j == n
7399
```
74100

75101
### **Java**
76102

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

79105
```java
106+
class Solution {
107+
public boolean validWordAbbreviation(String word, String abbr) {
108+
int m = word.length(), n = abbr.length();
109+
int i = 0, j = 0;
110+
while (i < m) {
111+
if (j >= n) {
112+
return false;
113+
}
114+
if (word.charAt(i) == abbr.charAt(j)) {
115+
++i;
116+
++j;
117+
continue;
118+
}
119+
int k = j;
120+
while (k < n && Character.isDigit(abbr.charAt(k))) {
121+
++k;
122+
}
123+
String t = abbr.substring(j, k);
124+
if (j == k || t.charAt(0) == '0' || Integer.parseInt(t) == 0) {
125+
return false;
126+
}
127+
i += Integer.parseInt(t);
128+
j = k;
129+
}
130+
return i == m && j == n;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
bool validWordAbbreviation(string word, string abbr) {
141+
int i = 0, j = 0;
142+
int m = word.size(), n = abbr.size();
143+
while (i < m) {
144+
if (j >= n) {
145+
return false;
146+
}
147+
if (word[i] == abbr[j]) {
148+
++i;
149+
++j;
150+
continue;
151+
}
152+
int k = j;
153+
while (k < n && isdigit(abbr[k])) {
154+
++k;
155+
}
156+
string t = abbr.substr(j, k - j);
157+
if (k == j || t[0] == '0') {
158+
return false;
159+
}
160+
int x = stoi(t);
161+
if (x == 0) {
162+
return false;
163+
}
164+
i += x;
165+
j = k;
166+
}
167+
return i == m && j == n;
168+
}
169+
};
170+
```
80171
172+
### **Go**
173+
174+
```go
175+
func validWordAbbreviation(word string, abbr string) bool {
176+
i, j := 0, 0
177+
m, n := len(word), len(abbr)
178+
for i < m {
179+
if j >= n {
180+
return false
181+
}
182+
if word[i] == abbr[j] {
183+
i++
184+
j++
185+
continue
186+
}
187+
k := j
188+
for k < n && abbr[k] >= '0' && abbr[k] <= '9' {
189+
k++
190+
}
191+
if k == j || abbr[j] == '0' {
192+
return false
193+
}
194+
x, _ := strconv.Atoi(abbr[j:k])
195+
if x == 0 {
196+
return false
197+
}
198+
i += x
199+
j = k
200+
}
201+
return i == m && j == n
202+
}
81203
```
82204

83205
### **...**

solution/0400-0499/0408.Valid Word Abbreviation/README_EN.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,127 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
68+
i = j = 0
69+
m, n = len(word), len(abbr)
70+
while i < m:
71+
if j >= n:
72+
return False
73+
if word[i] == abbr[j]:
74+
i, j = i + 1, j + 1
75+
continue
76+
k = j
77+
while k < n and abbr[k].isdigit():
78+
k += 1
79+
t = abbr[j: k]
80+
if not t.isdigit() or t[0] == '0' or int(t) == 0:
81+
return False
82+
i += int(t)
83+
j = k
84+
return i == m and j == n
6785
```
6886

6987
### **Java**
7088

7189
```java
90+
class Solution {
91+
public boolean validWordAbbreviation(String word, String abbr) {
92+
int m = word.length(), n = abbr.length();
93+
int i = 0, j = 0;
94+
while (i < m) {
95+
if (j >= n) {
96+
return false;
97+
}
98+
if (word.charAt(i) == abbr.charAt(j)) {
99+
++i;
100+
++j;
101+
continue;
102+
}
103+
int k = j;
104+
while (k < n && Character.isDigit(abbr.charAt(k))) {
105+
++k;
106+
}
107+
String t = abbr.substring(j, k);
108+
if (j == k || t.charAt(0) == '0' || Integer.parseInt(t) == 0) {
109+
return false;
110+
}
111+
i += Integer.parseInt(t);
112+
j = k;
113+
}
114+
return i == m && j == n;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
bool validWordAbbreviation(string word, string abbr) {
125+
int i = 0, j = 0;
126+
int m = word.size(), n = abbr.size();
127+
while (i < m) {
128+
if (j >= n) {
129+
return false;
130+
}
131+
if (word[i] == abbr[j]) {
132+
++i;
133+
++j;
134+
continue;
135+
}
136+
int k = j;
137+
while (k < n && isdigit(abbr[k])) {
138+
++k;
139+
}
140+
string t = abbr.substr(j, k - j);
141+
if (k == j || t[0] == '0') {
142+
return false;
143+
}
144+
int x = stoi(t);
145+
if (x == 0) {
146+
return false;
147+
}
148+
i += x;
149+
j = k;
150+
}
151+
return i == m && j == n;
152+
}
153+
};
154+
```
72155
156+
### **Go**
157+
158+
```go
159+
func validWordAbbreviation(word string, abbr string) bool {
160+
i, j := 0, 0
161+
m, n := len(word), len(abbr)
162+
for i < m {
163+
if j >= n {
164+
return false
165+
}
166+
if word[i] == abbr[j] {
167+
i++
168+
j++
169+
continue
170+
}
171+
k := j
172+
for k < n && abbr[k] >= '0' && abbr[k] <= '9' {
173+
k++
174+
}
175+
if k == j || abbr[j] == '0' {
176+
return false
177+
}
178+
x, _ := strconv.Atoi(abbr[j:k])
179+
if x == 0 {
180+
return false
181+
}
182+
i += x
183+
j = k
184+
}
185+
return i == m && j == n
186+
}
73187
```
74188

75189
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
bool validWordAbbreviation(string word, string abbr) {
4+
int i = 0, j = 0;
5+
int m = word.size(), n = abbr.size();
6+
while (i < m) {
7+
if (j >= n) {
8+
return false;
9+
}
10+
if (word[i] == abbr[j]) {
11+
++i;
12+
++j;
13+
continue;
14+
}
15+
int k = j;
16+
while (k < n && isdigit(abbr[k])) {
17+
++k;
18+
}
19+
string t = abbr.substr(j, k - j);
20+
if (k == j || t[0] == '0') {
21+
return false;
22+
}
23+
int x = stoi(t);
24+
if (x == 0) {
25+
return false;
26+
}
27+
i += x;
28+
j = k;
29+
}
30+
return i == m && j == n;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func validWordAbbreviation(word string, abbr string) bool {
2+
i, j := 0, 0
3+
m, n := len(word), len(abbr)
4+
for i < m {
5+
if j >= n {
6+
return false
7+
}
8+
if word[i] == abbr[j] {
9+
i++
10+
j++
11+
continue
12+
}
13+
k := j
14+
for k < n && abbr[k] >= '0' && abbr[k] <= '9' {
15+
k++
16+
}
17+
if k == j || abbr[j] == '0' {
18+
return false
19+
}
20+
x, _ := strconv.Atoi(abbr[j:k])
21+
if x == 0 {
22+
return false
23+
}
24+
i += x
25+
j = k
26+
}
27+
return i == m && j == n
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean validWordAbbreviation(String word, String abbr) {
3+
int m = word.length(), n = abbr.length();
4+
int i = 0, j = 0;
5+
while (i < m) {
6+
if (j >= n) {
7+
return false;
8+
}
9+
if (word.charAt(i) == abbr.charAt(j)) {
10+
++i;
11+
++j;
12+
continue;
13+
}
14+
int k = j;
15+
while (k < n && Character.isDigit(abbr.charAt(k))) {
16+
++k;
17+
}
18+
String t = abbr.substring(j, k);
19+
if (j == k || t.charAt(0) == '0' || Integer.parseInt(t) == 0) {
20+
return false;
21+
}
22+
i += Integer.parseInt(t);
23+
j = k;
24+
}
25+
return i == m && j == n;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
3+
i = j = 0
4+
m, n = len(word), len(abbr)
5+
while i < m:
6+
if j >= n:
7+
return False
8+
if word[i] == abbr[j]:
9+
i, j = i + 1, j + 1
10+
continue
11+
k = j
12+
while k < n and abbr[k].isdigit():
13+
k += 1
14+
t = abbr[j: k]
15+
if not t.isdigit() or t[0] == '0' or int(t) == 0:
16+
return False
17+
i += int(t)
18+
j = k
19+
return i == m and j == n

0 commit comments

Comments
 (0)