-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValid-Word-Abbreviation.py
More file actions
34 lines (30 loc) · 1001 Bytes
/
Valid-Word-Abbreviation.py
File metadata and controls
34 lines (30 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import re
class Solution(object):
def validWordAbbreviation(self, word, abbr):
def is_int(string):
try:
int(string)
return True
except:
return False
word = ' {} '.format(word)
abbrList = list(abbr)
newWord = ''
while (len(abbrList) > 0):
intVal = ''
while ((len(abbrList) > 0) and is_int(abbrList[0])):
intVal += abbrList.pop(0)
if (len(intVal) > 0):
if (intVal[0] == '0'):
return ''
if (int(intVal) > len(word)):
return ''
for i in range(int(intVal)):
newWord += '.'
if (len(abbrList) > 0):
newWord += abbrList.pop(0)
newWord = 's{}s'.format(newWord)
print word
print newWord
print re.findall(newWord, word)
return (len(re.findall(newWord, word)) > 0)