44. Wildcard Matching

Total Accepted: 59407 Total Submissions: 335592 Difficulty: Hard

Implement wildcard pattern matching with support for '?' and ''.
'?' Matches any single character.
'
' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "") → true
isMatch("aa", "a
") → true
isMatch("ab", "?") → true
isMatch("aab", "c
a*b") → false

Hide Company Tags Google Snapchat Facebook
Hide Tags Dynamic Programming Backtracking Greedy String
Hide Similar Problems (H) Regular Expression Matching

public class Solution {
    public boolean isMatch(String s, String p) {
        // ref: dp solution: https://www.youtube.com/watch?v=3ZDZ-N0EPV0
        char[] str = s.toCharArray();
        char[] pattern = p.toCharArray();
        
        // replace multiple * with one *
        // e.g. a***b***c --> a*b*c
        boolean isFirst = true;
        int writeIndex = 0;
        
        for (int i = 0; i < pattern.length; i++) {
           if (pattern[i] == '*') {
               if (isFirst) {
                    pattern[writeIndex++] = pattern[i];
                    isFirst = false;
                }
            } else {
                pattern[writeIndex++] = pattern[i];
                isFirst = true;
            }
        }
        
        // str - > row,  pattern -> col,  2D matrix, dp solution 
        /*  T[i][j]  =  (1) T[i-1][j-1]              if p[j] = ? || s[i-1] == p[j-1]
                        (2)  T[i][j-1] || T[i-1][j]  if p[j]=*
                        (3)  False                   if s[i] != p[j]
            Time complexity : O(m * n)   Space complexity : O(m * n)
            s = "xbylmz" , p = "x?y*z" --> true
        */
        boolean[][] T = new boolean[str.length + 1][pattern.length + 1];
        T[0][0] = true;
        
        if (writeIndex > 0 && pattern[0] == '*') {
            T[0][1] = true;
        }
        
        for (int i = 1; i < T.length; i++) {
            for (int j = 1; j < T[0].length; j++) {
                if (pattern[j-1] == '?'  || str[i-1] == pattern[j-1]  ) {
                    T[i][j] = T[i-1][j-1];
                } else if (pattern[j-1] == '*') {
                    T[i][j] =  T[i-1][j] || T[i][j-1];
                }
            }
        }
        return T[str.length][writeIndex];
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 問題 Implement wildcard pattern matching with support for '...
    RobotBerry閱讀 264評論 0 0
  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,776評論 0 33
  • 去年我就聽了王芳老師的這節課,現在再聽一次收獲良多,值得推薦給大家。王芳是著名金牌主持人,媒體人、教育人。 擅長啟...
    成長書房閱讀 634評論 0 2
  • 今天,剽悍晨讀分享的是《極簡生活,成就真正的幸福與精彩》,感謝貓爺。 (《極簡生活》由日本有川真由美所著) 讀后別...
    孫國飛揚閱讀 170評論 0 4
  • 休養期間的飲食一隅 馬鈴薯燜肉片?好侍咖喱 南瓜連皮燜肉片 蝦皮蒸水蛋 十六谷米100克瘦肉塊還有紅貝子丁瘦肉湯 ...
    lianni馮閱讀 182評論 0 0