Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 2.01 KB

_408. Valid Word Abbreviation.md

File metadata and controls

73 lines (54 loc) · 2.01 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 02, 2024

Last updated : July 01, 2024


Related Topics : Two Pointers, String

Acceptance Rate : 35.876 %


Solutions

Python

# This is NOT an easy question lmao
# It's not hard, it just has wayyyyy too many edge cases that can only be found via submitting sigh

class Solution:
    def validWordAbbreviation(self, word: str, abbr: str) -> bool:
        if len(word) == 1 :
            return abbr == word or abbr == '1'

        strs = [x for x in re.split(r'\d', abbr) if not x == '']
        vals = [x for x in re.findall(r'\d+', abbr) if not x == '']

        for i in vals:
            if i[0] == '0':
                return False

        if len(strs) == 0:
            return int(vals[0]) == len(word)

        index = 0
        strsIndex = 0
        valsIndex = 0
        currentlyStrs = word[0:len(strs[0])] == strs[0]

        while strsIndex < len(strs) or valsIndex < len(vals) :
            if currentlyStrs :
                if strsIndex >= len(strs) :
                    return False
                if word[index:index + len(strs[strsIndex])] == strs[strsIndex] :
                    index += len(strs[strsIndex])
                    strsIndex += 1
                    currentlyStrs = not currentlyStrs
                else :
                    return False
            else :
                if valsIndex >= len(vals) :
                    return False
                index += int(vals[valsIndex])
                valsIndex += 1
                if index > len(word) :
                    return False
                currentlyStrs = not currentlyStrs
        return len(word) == index