-
Notifications
You must be signed in to change notification settings - Fork 0
/
408-ValidWordAbbreviation.py
45 lines (31 loc) · 1.03 KB
/
408-ValidWordAbbreviation.py
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
34
35
36
37
38
39
40
41
42
43
44
45
class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
# Two pointers
i, j = 0, 0
jump = 0
jump_str = ""
valid_nums = [str(i) for i in range(0, 10)]
# While word finishes
while i < len(word) and j < len(abbr):
if abbr[j] in valid_nums:
jump_str += abbr[j]
j += 1
elif jump_str != "":
if jump_str[0] == "0":
return False
jump = int(jump_str)
jump_str = ""
i += jump
if i > len(word):
return False
elif word[i] == abbr[j]:
i += 1
j += 1
elif word[i] != abbr[j]:
return False
if jump_str != "" and jump_str[0] != "0":
jump = int(jump_str)
i += jump
if j == len(abbr) and i == len(word):
return True
return False