class Solution:
"""
@param word: a non-empty string
@param abbr: an abbreviation
@return: true if string matches with the given abbr or false
"""
def validWordAbbreviation(self, word, abbr):
# write your code here
#thinking process
#we keep two pointer
#pointer to track where we are on abbr
#pointer to track where we are on word
#if abbr is letter, than compare
#if abbr is digit then, grab the whole number and increase j by the num.
#* to get the whole num value refer to Programming trick
if not word or not abbr:
return False
i = 0 #pointer for the abbr
j = 0 #pointer used for compare with word
if len(word) < len(abbr):
return False
# "w1rd"
# "word"
while i < len(abbr):
if abbr[i].isdigit():
num = 0
count = 1
while i < len(abbr) and abbr[i].isdigit():
num = num * count + int(abbr[i])
i += 1
count *= 10
j += num
#current abbr is a letter
else:
if abbr[i] != word[j]:
return False
i += 1
j += 1
if i == len(abbr) and j == len(word):
return True
return False