Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*' where:
class Solution:
def isMatch(self, s: str, p: str) -> bool:
result = False
i = 0
j = 0
while i < len(s)-2 and j < len(p)-1:
k = i
if s[i] == p[j] and p[j+1] == '*':
while s[i] == s[i+1]:
i += 1
else:
i += 1
j += 1
elif p[j] == '.':
continue
else:
if p[j+1] == '*':
j += 2
if i < len(s) or j < len(p):
return False
else:
return True
경우를 다 따져서 조건문 범벅으로 하려고 했으나...
범위 제한하는게 너무 길어져서 포기함...ㅎ
class Solution:
def isMatch(self, s: str, p: str) -> bool:
if not p:
return not s
# s 가 빈 문자열인지 아닌지 & p[0] 가 s[0] or . 인지 확인 => T/F
first_match = bool(s) and p[0] in {s[0], '.'}
# s 에서 반복되는 부분만큼 재귀 돌려서 찾기
if len(p) >= 2 and p[1] == '*':
return (self.isMatch(s, p[2:]) or
first_match and self.isMatch(s[1:], p))
else:
# [0] 부분이 match 되면 다음 부분을 재귀 돌림
return first_match and self.isMatch(s[1:], p[1:])
if len(p) >= 2 and p[1] == '*':
=> 반복되는 부분 찾기
else:
=> 다음 부분의 match 찾기