LintCode 637 LeetCode 408 – Python 3 (Week 09 – 06)

 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, 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':
                    start = j
                    while j < len(abbr) and abbr[j].isdigit():
                        j += 1
                    i = i + int(abbr[start : j])
                else:
                    return False
        
        return i == len(word) and j == len(abbr)

Time complexity is O(nm). Space complexity is O(1).

Pay attention to that abbr[j] is string. The first digit should not be 0.

Leave a comment

Design a site like this with WordPress.com
Get started