Two Pointers Python Solution


  • 1
    class Solution(object):
        def validWordAbbreviation(self, word, abbr):
            """
            :type word: str
            :type abbr: str
            :rtype: bool
            """
            i, j = 0, 0
            while i < len(abbr):
                if j >= len(word):
                    return False
                if not abbr[i].isdigit():
                    if abbr[i] != word[j]:
                        return False
                    i += 1
                    j += 1
                else:
                    if abbr[i] == '0':
                        return False
                    n = ''
                    while i < len(abbr) and abbr[i].isdigit():
                        n += abbr[i]
                        i += 1
                    j += int(n)
            return j == len(word)

  • 0
    F
    This post is deleted!

Log in to reply
 

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