Skip to content

Commit 8565822

Browse files
committed
pointers
1 parent 46e4555 commit 8565822

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

408 Valid Word Abbreviation.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Premium Question
3+
"""
4+
__author__ = 'Daniel'
5+
6+
7+
class Solution(object):
8+
def validWordAbbreviation(self, word, abbr):
9+
"""
10+
pointers
11+
:type word: str
12+
:type abbr: str
13+
:rtype: bool
14+
"""
15+
w = 0
16+
a = 0
17+
while w < len(word) and a < len(abbr):
18+
if abbr[a].isdigit() and abbr[a] != '0':
19+
e = a
20+
while e < len(abbr) and abbr[e].isdigit(): e += 1
21+
num = int(abbr[a:e])
22+
a = e
23+
w += num
24+
else:
25+
if word[w] != abbr[a]:
26+
return False
27+
28+
w += 1
29+
a += 1
30+
31+
return w == len(word) and a == len(abbr)
32+
33+
34+
if __name__ == "__main__":
35+
assert Solution().validWordAbbreviation("internationalization", "i12iz4n") == True
36+
assert Solution().validWordAbbreviation("apple", "a2e") == False

0 commit comments

Comments
 (0)