Valid Word Abbreviation
字符串的处理实在太脏了,有时候不是写不出来,而是五花八门的corner case使得逻辑无法干净,因为无法一次性考虑这么多的边界情况,不过双指针比对的题,现在看起来最外层循环条件还是把两个指针的判断都放进去最好
class Solution(object):
def validWordAbbreviation(self, word, abbr):
"""
:type word: str
:type abbr: str
:rtype: bool
"""
n, m = len(word), len(abbr)
curWord, curAbbr = 0, 0
while curWord < n and curAbbr < m:
if word[curWord] == abbr[curAbbr]:
curWord += 1
curAbbr += 1
continue
if abbr[curAbbr] == '0' or not abbr[curAbbr].isdigit():
return False
curVal = 0
while curAbbr < m and abbr[curAbbr].isdigit():
curVal = 10 * curVal + ord(abbr[curAbbr]) - ord('0')
curAbbr += 1
curWord += curVal
return curWord == n and curAbbr == m