637. Valid Word Abbreviation
给定一个非空字符串 word
和缩写 abbr
,返回字符串是否可以和给定的缩写匹配。
比如一个 “word”
的字符串仅包含以下有效缩写:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
样例
样例 1:
给出 s = "internationalization", abbr = "i12iz4n":
返回 true。
样例 2:
给出 s = "apple", abbr = "a2e":
返回 false。
注意事项
注意只有以上缩写是字符串 word
合法的缩写。任何其他关于 word
的缩写都是不合法的。
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
i = j = 0
while i < len(word) and j < len(abbr):
if word[i] == abbr[j]:
i += 1
j += 1
elif abbr[j].isdigit() and abbr[j] != '0':
num = ''
while j < len(abbr) and abbr[j].isdigit():
num += abbr[j]
j += 1
i += int(num)
else:
return False
return i == len(word)