Easy
Valid Word Abbreviation — Python
Full explanation · Time O(n) · Space O(1)
# Time: O(n)
# Space: O(1)
class Solution(object):
def validWordAbbreviation(self, word, abbr):
"""
:type word: str
:type abbr: str
:rtype: bool
"""
i , digit = 0, 0
for c in abbr:
if c.isdigit():
if digit == 0 and c == '0':
return False
digit *= 10
digit += int(c)
else:
if digit:
i += digit
digit = 0
if i >= len(word) or word[i] != c:
return False
i += 1
if digit:
i += digit
return i == len(word)