Scribbling

44. Wildcard Matching 본문

Computer Science/Coding Test

44. Wildcard Matching

focalpoint 2021. 9. 6. 15:41

생각보다는 쉽게 풀린다.

 

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        
        n = len(p)
        m = len(s)
        
        # matched[i][j]: (i-1)th pattern matches (j-1)th string
        matched = [[False] * (m+1) for _ in range(n+1)]
        
        matched[0][0] = True
        for j in range(1, m+1):
            matched[0][j] = False
        
        for i in range(1, n+1):
            if p[i-1] == '*':
                matched[i][0] = matched[i-1][0]
        
        for i in range(1, n+1):
            for j in range(1, m+1):
                if p[i-1] == '*':
                    matched[i][j] = matched[i][j-1] or matched[i-1][j]
                elif p[i-1] == '?':
                    matched[i][j] = matched[i-1][j-1]
                else:
                    matched[i][j] = matched[i-1][j-1] and p[i-1] == s[j-1]
                    
        return matched[n][m]

'Computer Science > Coding Test' 카테고리의 다른 글

37. Sudoku Solver  (0) 2021.09.06
36. Valid Sudoku  (0) 2021.09.06
LeetCode: 48. Rotate Image  (0) 2021.09.05
39. Combination Sum  (0) 2021.09.04
32. Longest Valid Parentheses  (0) 2021.09.04