|
62 | 62 |
|
63 | 63 | <!-- 这里可写通用的实现逻辑 -->
|
64 | 64 |
|
| 65 | +**方法一:模拟** |
| 66 | + |
| 67 | +模拟字符匹配替换。 |
| 68 | + |
| 69 | +同时遍历 $word$ 和 $abbr$,若 $abbr$ 遇到数字,则 $word$ 跳过对应数字长度的字符数。若数字为空,或者有前导零,则提前返回 false。 |
| 70 | + |
| 71 | +时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。其中 $m$ 是 $word$ 的长度,$n$ 是 $abbr$ 的长度。 |
| 72 | + |
65 | 73 | <!-- tabs:start -->
|
66 | 74 |
|
67 | 75 | ### **Python3**
|
68 | 76 |
|
69 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
70 | 78 |
|
71 | 79 | ```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 |
73 | 99 | ```
|
74 | 100 |
|
75 | 101 | ### **Java**
|
76 | 102 |
|
77 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 104 |
|
79 | 105 | ```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 | +``` |
80 | 171 |
|
| 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 | +} |
81 | 203 | ```
|
82 | 204 |
|
83 | 205 | ### **...**
|
|
0 commit comments