Abbreviation
408 Valid Word Abbreviation
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
if not abbr:
return not word
if not word:
return False
if abbr[0].isalpha():
if abbr[0] != word[0]: return False
else:
return self.validWordAbbreviation(word[1:], abbr[1:])
else:
num = int(abbr[0])
# cornor case
# ['a'], ['01']
if num == 0: return False
i = 1
while(i < len(abbr) and abbr[i].isdigit()):
num = num*10 + int(abbr[i])
i+=1
return num <= len(word) and self.validWordAbbreviation(word[num:], abbr[i:])320. Generalize Abbreviation
527 Word Abbreviation
Last updated