408. Valid Word Abbreviation
Input: word = "internationalization", abbr = "i12iz4n"
Output: true
Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").Last updated
Input: word = "internationalization", abbr = "i12iz4n"
Output: true
Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").Last updated
Input: word = "apple", abbr = "a2e"
Output: false
Explanation: The word "apple" cannot be abbreviated as "a2e".class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
# Input: word = "internationalization", abbr = "i12iz4n"
pos = 0
num = 0
for i,c in enumerate(abbr):
if c.isdigit():
if num == 0 and c == '0':
return False
num = num * 10 + int(c)
else:
pos += num
num = 0
if pos >= len(word) or word[pos] != c:
return False
pos += 1
return pos + num == len(word) #别忘了最后的NUMBER!!!!!!!