64
64
65
65
** 方法一:模拟**
66
66
67
- 模拟字符匹配替换 。
67
+ 我们可以直接模拟字符匹配替换 。
68
68
69
- 同时遍历 $word$ 和 $abbr$,若 $abbr$ 遇到数字,则 $ word$ 跳过对应数字长度的字符数。若数字为空,或者有前导零,则提前返回 false 。
69
+ 假设字符串 $word$ 和字符串 $abbr$ 的长度分别为 $m$ 和 $n$,我们使用两个指针 $i$ 和 $j$ 分别指向字符串 $ word$ 和字符串 $abbr$ 的初始位置,用一个整型变量 $x$ 记录当前匹配到的 $abbr$ 的数字 。
70
70
71
- 时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。其中 $m$ 是 $word$ 的长度,而 $n$ 是 $abbr$ 的长度。
71
+ 循环匹配字符串 $word$ 和字符串 $abbr$ 的每个字符:
72
+
73
+ 如果指针 $j$ 指向的字符 $abbr[ j] $ 是数字,如果 $abbr[ j] $ 是 ` '0' ` ,并且 $x$ 为 $0$,说明 $abbr$ 中的数字含有前导零,因此不是合法的缩写,返回 ` false ` ;否则将 $x$ 更新为 $x \times 10 + abbr[ j] - '0'$。
74
+
75
+ 如果指针 $j$ 指向的字符 $abbr[ j] $ 不是数字,那么我们此时将指针 $i$ 往前移动 $x$ 个位置,然后将 $x$ 重置为 $0$。如果此时 $i \geq m$ 或者 $word[ i] \neq abbr[ j] $,说明两个字符串无法匹配,返回 ` false ` ;否则将指针 $i$ 往前移动 $1$ 个位置。
76
+
77
+ 然后我们将指针 $j$ 往前移动 $1$ 个位置,重复上述过程,直到 $i$ 超出字符串 $word$ 的长度或者 $j$ 超出字符串 $abbr$ 的长度。
78
+
79
+ 最后,如果 $i + x$ 等于 $m$ 且 $j$ 等于 $n$,说明字符串 $word$ 可以缩写成字符串 $abbr$,返回 ` true ` ;否则返回 ` false ` 。
80
+
81
+ 时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是字符串 $word$ 和字符串 $abbr$ 的长度。空间复杂度 $O(1)$。
72
82
73
83
<!-- tabs:start -->
74
84
79
89
``` python
80
90
class Solution :
81
91
def validWordAbbreviation (self , word : str , abbr : str ) -> bool :
82
- i = j = 0
83
92
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
93
+ i = j = x = 0
94
+ while i < m and j < n:
95
+ if abbr[j].isdigit():
96
+ if abbr[j] == " 0" and x == 0 :
97
+ return False
98
+ x = x * 10 + int (abbr[j])
99
+ else :
100
+ i += x
101
+ x = 0
102
+ if i >= m or word[i] != abbr[j]:
103
+ return False
104
+ i += 1
105
+ j += 1
106
+ return i + x == m and j == n
99
107
```
100
108
101
109
### ** Java**
@@ -106,28 +114,24 @@ class Solution:
106
114
class Solution {
107
115
public boolean validWordAbbreviation (String word , String abbr ) {
108
116
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)) {
117
+ int i = 0 , j = 0 , x = 0 ;
118
+ for (; i < m && j < n; ++ j) {
119
+ char c = abbr. charAt(j);
120
+ if (Character . isDigit(c)) {
121
+ if (c == ' 0' && x == 0 ) {
122
+ return false ;
123
+ }
124
+ x = x * 10 + (c - ' 0' );
125
+ } else {
126
+ i += x;
127
+ x = 0 ;
128
+ if (i >= m || word. charAt(i) != c) {
129
+ return false ;
130
+ }
115
131
++ i;
116
- ++ j;
117
- continue ;
118
132
}
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
133
}
130
- return i == m && j == n;
134
+ return i + x == m && j == n;
131
135
}
132
136
}
133
137
```
@@ -138,33 +142,24 @@ class Solution {
138
142
class Solution {
139
143
public:
140
144
bool validWordAbbreviation(string word, string abbr) {
141
- int i = 0, j = 0;
142
145
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] ) {
146
+ int i = 0, j = 0, x = 0;
147
+ for (; i < m && j < n; ++j) {
148
+ if (isdigit(abbr[ j] )) {
149
+ if (abbr[ j] == '0' && x == 0) {
150
+ return false;
151
+ }
152
+ x = x * 10 + (abbr[ j] - '0');
153
+ } else {
154
+ i += x;
155
+ x = 0;
156
+ if (i >= m || word[ i] != abbr[ j] ) {
157
+ return false;
158
+ }
148
159
++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
160
}
164
- i += x;
165
- j = k;
166
161
}
167
- return i == m && j == n;
162
+ return i + x == m && j == n;
168
163
}
169
164
};
170
165
```
@@ -173,32 +168,48 @@ public:
173
168
174
169
```go
175
170
func validWordAbbreviation(word string, abbr string) bool {
176
- i, j := 0, 0
177
171
m, n := len(word), len(abbr)
178
- for i < m {
179
- if j >= n {
180
- return false
181
- }
182
- if word[i] == abbr[j] {
172
+ i, j, x := 0, 0, 0
173
+ for ; i < m && j < n; j++ {
174
+ if abbr[j] >= '0' && abbr[j] <= '9' {
175
+ if x == 0 && abbr[j] == '0' {
176
+ return false
177
+ }
178
+ x = x*10 + int(abbr[j]-'0')
179
+ } else {
180
+ i += x
181
+ x = 0
182
+ if i >= m || word[i] != abbr[j] {
183
+ return false
184
+ }
183
185
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
186
}
194
- x, _ := strconv.Atoi(abbr[j:k])
195
- if x == 0 {
196
- return false
197
- }
198
- i += x
199
- j = k
200
187
}
201
- return i == m && j == n
188
+ return i+x == m && j == n
189
+ }
190
+ ```
191
+
192
+ ### ** TypeScript**
193
+
194
+ ``` ts
195
+ function validWordAbbreviation(word : string , abbr : string ): boolean {
196
+ const [m, n] = [word .length , abbr .length ];
197
+ let [i, j, x] = [0 , 0 , 0 ];
198
+ for (; i < m && j < n ; ++ j ) {
199
+ if (abbr [j ] >= ' 0' && abbr [j ] <= ' 9' ) {
200
+ if (abbr [j ] === ' 0' && x === 0 ) {
201
+ return false ;
202
+ }
203
+ x = x * 10 + Number (abbr [j ]);
204
+ } else {
205
+ i += x ;
206
+ x = 0 ;
207
+ if (i >= m || word [i ++ ] !== abbr [j ]) {
208
+ return false ;
209
+ }
210
+ }
211
+ }
212
+ return i + x === m && j === n ;
202
213
}
203
214
```
204
215
0 commit comments