题目:
思路:
这个题目的描述就挺抽象的,然后我以为p中,只会出现一次“*”,结果测试用例里面有多个“*”的,而且C*,代表的是(0~n)C,C*是一个整体,我以为是“*”代表(0~n)个C,麻了,最后想了很久也没有很好的解决方案,有点其它的思路,但是感觉要重写,暂时先缓缓
代码:
class Solution:
def isMatch(self, s: str, p: str) -> bool:
mi_position = 0
position = 0
temp_list = []
p_length = len(p)
s_length = len(s)
length = s_length-p_length
flag_mi = True
temp_list2 = ''
while p[position] != "*": #寻找p中*的位置
position += 1
if position==p_length:
flag_mi =False #如果p中不存在“*”,flag为False
break
if flag_mi : #如果p中存在“*”
if length >=0: #s长度大于p
for i in range(length+1):
temp_list.append(p[position-1])
temp_str =''.join(''.join(row) for row in temp_list)
temp_list2 = p.split('*')
temp_list2.insert(1,temp_str)
temp_str2 =''.join(''.join(row) for row in temp_list2)
elif length ==-1:
temp_list2 = p.strip('*')
temp_str2 = ''.join(''.join(row) for row in temp_list2)
else:
return False
else:
temp_str2 =p
if len(temp_str2)!=s_length:
return False
for k in range(s_length):
if s[k] !=temp_str2[k] and temp_str2[k] !=".":
return False
return True