-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path408_Valid_Work_Abbreviation.py
More file actions
33 lines (29 loc) · 1.04 KB
/
Copy path408_Valid_Work_Abbreviation.py
File metadata and controls
33 lines (29 loc) · 1.04 KB
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
33
class Solution(object):
# Time: O(N), Space: O(1)
def validWordAbbreviation(self, word, abbr):
"""
:type word: str
:type abbr: str
:rtype: bool
"""
wordPointer = 0
abbrPointer = 0
while wordPointer < len(word) and abbrPointer < len(abbr):
if abbr[abbrPointer].isalpha():
if abbr[abbrPointer] != word[wordPointer]:
return False
abbrPointer += 1
wordPointer += 1
else:
if abbr[abbrPointer] == '0':
return False
tempValue = 0
while abbrPointer < len(abbr) and abbr[abbrPointer].isdigit():
tempValue = tempValue * 10 + int(abbr[abbrPointer])
abbrPointer += 1
wordPointer += tempValue
return abbrPointer == len(abbr) and wordPointer == len(word)
soln = Solution()
word = "internationalization"
abbr = "i12iz4n"
print(soln.validWordAbbreviation(word, abbr))