Advertisement
smj007

Valid Word Abbreviation

Aug 3rd, 2024
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. class Solution:
  2.     def validWordAbbreviation(self, word: str, abbr: str) -> bool:
  3.        
  4.         first = 0
  5.         second = 0
  6.  
  7.         while (first < len(word) and second < len(abbr)):
  8.             if word[first] == abbr[second]:
  9.                 first += 1
  10.                 second += 1
  11.             elif (abbr[second] == '0'):
  12.                 return False
  13.             elif abbr[second].isnumeric():
  14.                 count = 0
  15.                 while (second<len(abbr) and abbr[second].isnumeric()):
  16.                     count = count*10 + int(abbr[second])
  17.                     second += 1
  18.                 first = first + count
  19.             else:
  20.                 return False # what if characters don't match; what if the
  21.  
  22.         return first == len(word) and second == len(abbr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement