-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0408-valid-word-abbreviation.py
More file actions
32 lines (29 loc) · 938 Bytes
/
0408-valid-word-abbreviation.py
File metadata and controls
32 lines (29 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# time complexity: O(n)
# space complexity: O(1)
class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
i = j = 0
while j < len(abbr) and i < len(word):
if abbr[j].isalpha():
if abbr[j] != word[i]:
return False
i += 1
j += 1
else:
if abbr[j] == '0':
return False
temp = 0
while j < len(abbr) and abbr[j].isdigit():
temp = temp * 10 + int(abbr[j])
j += 1
i += temp
return j == len(abbr) and i == len(word)
word = "internationalization"
abbr = "i12iz4n"
print(Solution().validWordAbbreviation(word, abbr))
word = "apple"
abbr = "a2e"
print(Solution().validWordAbbreviation(word, abbr))
word = "abbde"
abbr = "a1b01e"
print(Solution().validWordAbbreviation(word, abbr))