valid_word_abbreviation
1# @leet start 2class Solution: 3 def validWordAbbreviation(self, word: str, abbr: str) -> bool: 4 """ 5 This question asks us to see if a pattern matches a word, where 6 numbers inside the pattern means match any one character. 7 8 So, we have to parse the abbreviation, without allowing leading zeroes, 9 and if we see a character, we check if it matches and increment pointers 10 if it does, otherwise, if its a number, parse the number and then skip 11 the word pointer that many characters. If the characters doesn't match, 12 return False, and at the end of the loop, if we haven't hit the end 13 of both of the strings, return False. 14 """ 15 i = j = 0 16 m, n = len(word), len(abbr) 17 18 while i < m and j < n: 19 if word[i] == abbr[j]: 20 i += 1 21 j += 1 22 elif abbr[j] == "0": 23 return False 24 elif abbr[j].isdigit(): 25 k = j 26 while k < n and abbr[k].isdigit(): 27 k += 1 28 i += int(abbr[j:k]) 29 j = k 30 else: 31 return False 32 return i == m and j == n 33 34 35# @leet end 36 37 38def test(): 39 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def validWordAbbreviation(self, word: str, abbr: str) -> bool: 5 """ 6 This question asks us to see if a pattern matches a word, where 7 numbers inside the pattern means match any one character. 8 9 So, we have to parse the abbreviation, without allowing leading zeroes, 10 and if we see a character, we check if it matches and increment pointers 11 if it does, otherwise, if its a number, parse the number and then skip 12 the word pointer that many characters. If the characters doesn't match, 13 return False, and at the end of the loop, if we haven't hit the end 14 of both of the strings, return False. 15 """ 16 i = j = 0 17 m, n = len(word), len(abbr) 18 19 while i < m and j < n: 20 if word[i] == abbr[j]: 21 i += 1 22 j += 1 23 elif abbr[j] == "0": 24 return False 25 elif abbr[j].isdigit(): 26 k = j 27 while k < n and abbr[k].isdigit(): 28 k += 1 29 i += int(abbr[j:k]) 30 j = k 31 else: 32 return False 33 return i == m and j == n
def
validWordAbbreviation(self, word: str, abbr: str) -> bool:
4 def validWordAbbreviation(self, word: str, abbr: str) -> bool: 5 """ 6 This question asks us to see if a pattern matches a word, where 7 numbers inside the pattern means match any one character. 8 9 So, we have to parse the abbreviation, without allowing leading zeroes, 10 and if we see a character, we check if it matches and increment pointers 11 if it does, otherwise, if its a number, parse the number and then skip 12 the word pointer that many characters. If the characters doesn't match, 13 return False, and at the end of the loop, if we haven't hit the end 14 of both of the strings, return False. 15 """ 16 i = j = 0 17 m, n = len(word), len(abbr) 18 19 while i < m and j < n: 20 if word[i] == abbr[j]: 21 i += 1 22 j += 1 23 elif abbr[j] == "0": 24 return False 25 elif abbr[j].isdigit(): 26 k = j 27 while k < n and abbr[k].isdigit(): 28 k += 1 29 i += int(abbr[j:k]) 30 j = k 31 else: 32 return False 33 return i == m and j == n
This question asks us to see if a pattern matches a word, where numbers inside the pattern means match any one character.
So, we have to parse the abbreviation, without allowing leading zeroes, and if we see a character, we check if it matches and increment pointers if it does, otherwise, if its a number, parse the number and then skip the word pointer that many characters. If the characters doesn't match, return False, and at the end of the loop, if we haven't hit the end of both of the strings, return False.
def
test():