본문 바로가기
<개인공부> - IT/[Python]

Python in operator (Regular Expression Matching)

by Aggies '19 2019. 1. 10.
반응형

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/

반응형