Nowadays I really focus on preparing coding interview. I am going to briefly summarize and post what I learn and study.
text = "aa"
pattern = "a"
# Whether pattern is null or not
# if it is null return False
if not pattern:
return not text
# Input text is not null
# Pattern has the same character or '.'
first_match = bool(text) and pattern[0] in {text[0], '.'}
if len(pattern) >= 2 and pattern[1] == '*':
# self.isMatch(text, pattern[2:] -> ".*" case checks
# self.isMatch(text[1:], pattern) -> ".*abcd" case checks
return (self.isMatch(text, pattern[2:]) or
first_match and self.isMatch(text[1:], pattern))
# self.isMatch(text[1:], pattern[1:] -> keep matching
# example input text = "aaa"
# "aaa" -> "aa" -> "a" -> null
else:
return first_match and self.isMatch(text[1:], pattern[1:])
Reference site : https://leetcode.com/problems/regular-expression-matching/solution/
'<개인공부> - IT > [Python]' 카테고리의 다른 글
13. Roman to Integer (0) | 2019.01.12 |
---|---|
Two pointer approach (11. Container with most water) (0) | 2019.01.11 |
What does if __name__ == “__main__”: do? (0) | 2018.09.15 |
Python Multiple return values (Tuple, Dict) (0) | 2018.08.23 |
List 자료형의 Join함수 사용하기 (0) | 2018.08.21 |