Python


  • 0
    B
        def validWordAbbreviation(self, word, abbr):
            """
            :type word: str
            :type abbr: str
            :rtype: bool
            """
            if len(abbr) > len(word): return False # Needed only for optimization, does not affect functionality
            if abbr.startswith('0'): return False # Same
            left, leftabbr = 0, 0
            while True:
                    prev = leftabbr
                    if leftabbr <= len(abbr)-1 and abbr[leftabbr].isdigit():
                            if abbr[leftabbr] == '0': return False
                            while leftabbr <= len(abbr)-1 and abbr[leftabbr].isdigit():
                                    leftabbr += 1
                            cnt = int(abbr[prev:leftabbr])
                            left += cnt
                    if left > len(word): return False
                    if left >= len(word) and leftabbr < len(abbr): return False
                    if left <= len(word)-1 and leftabbr > len(abbr)-1: return False
                    if left > len(word)-1: break
                    if word[left] != abbr[leftabbr]: return False
                    left += 1
                    leftabbr += 1
            return True
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.