题目

解析:状态机实现正则,NP的实现应该更优,但感觉还是状态机实现是最适合的

class Solution:
    def isMatch(self, s: str, p: str) -> bool:

        def next_match(p, state, next_state):
            next_state.append(state)
            if state + 1 < len(p) and p[state+1] == "*":
                next_match(p, state+2, next_state)

        next_state = []
        index = 0
        next_match(p, 0, next_state)
        while len(next_state) > 0:
            now_state = next_state
            next_state = []
            for state in now_state:
                if state >= len(p) and index >= len(s):
                    return True

                elif state >= len(p):
                    continue

                elif index < len(s):
                    if p[state] == "." or s[index] == p[state]:
                        if state + 1 < len(p) and p[state+1] == "*":
                            next_match(p, state, next_state)
                        else:
                            next_match(p, state+1, next_state)

            index += 1
        return False