LC 0408 [E] Valid Word Abbreviation - ALawliet/algorithms GitHub Wiki
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
w = 0
a = 0
while w < len(word) and a < len(abbr):
if abbr[a].isalpha():
if word[w] != abbr[a]:
return False
w += 1
a += 1
else: # abbr[a].isdigit()
# leading 0 edge case
if abbr[a] == '0':
return False
# count the number of consecutive skips with a, store the number as a str, then convert it to int later
num_skips_a_str = ''
while a < len(abbr) and abbr[a].isdigit():
num_skips_a_str += abbr[a]
a += 1
# add the skips to word pointer
w += int(num_skips_a_str)
return w == len(word) and a == len(abbr) # reached the end of both word and abbrev