Leetcode 44:Wildcard Matching

来源:互联网 发布:现在开淘宝店好做吗 编辑:程序博客网 时间:2024/05/22 14:37

Leetcode 44:Wildcard Matching

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

分析

与 Leetcode 10 的 一丢丢关系
其实它们很像,不同在于 一些字符代表的含义变了
? : 匹配任意单个字符
* :匹配任意任意字符串,包括空字符串

所以,相应的,我们的dp的递推公式也有变化
dp[i][j] :

  1. 若p[j-1] 为 *
    匹配空字符串:s[0…i-1] p[0…j-2] 已匹配,则

    dp[i][j] = dp[i][j-1]

    匹配非空字符串:s[0…i-2] p[0…j-1] 已匹配,则

    dp[i][j] = dp[i-1][j]
  2. 若p[j-1] 不为 *

    dp[i][j] = dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '?') 

解答


dp

public class Solution {    public boolean isMatch(String s, String p) {        boolean[][] dp = new boolean[s.length()+1][p.length()+1];        dp[0][0] = true;        //dp[0][j]        for(int j=1;j<p.length()+1;j++){            if(p.charAt(j-1) == '*'){                dp[0][j]=dp[0][j-1];            }        }        for(int i=1;i<s.length()+1;i++){            for(int j=1;j<p.length()+1;j++){                if(p.charAt(j-1) == '*'){                    dp[i][j] = dp[i][j-1] || dp[i-1][j];                }                else {                    dp[i][j] = dp[i-1][j-1] && (s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1)=='?');                }            }        }        return dp[s.length()][p.length()];    }}

不用dp也还可以,双指针法

假设我们用两个指针分别指向s和p字符串中要匹配的位置,首先分析下通配符匹配过程中会有哪些情况是成功:

  1. s的字符和p的字符相等
  2. p中的字符是?,这时无论s的字符是什么都可以匹配一个
  3. p中遇到了一个*,这时无论s的字符是什么都没关系
  4. 之前的都不符合,但是p在之前的位置有一个,我们可以从上一个后面开始匹配
  5. s已经匹配完,但是p后面还有很多连续的`*

这里1和2的情况比较好处理,关键在于如何处理3和4的情况。当我们遇到一个时,因为之后可能要退回至该位置重新匹配,我们要将它的下标记录下来,比如idxstar。但是,当我们连续遇到两次4的情况,如何保证我还是能继续匹配s,而不是每次都退回idxstar+1导致循环呢?所以我们还要记录一个idxmatch,用来记录用上一个连续匹配到的s中的下标。最后,对于情况5,我们用一个循环跳过末尾的*跳过就行了。

public class Solution {    public boolean isMatch(String s, String p) {        int sIndex =0 , pIndex = 0, starIndex = -1;        int sMatchStar = 0;        while(sIndex<s.length()){            // 当两个指针指向完全相同的字符时,或者p中遇到的是?时            if(pIndex < p.length() && (p.charAt(pIndex)==s.charAt(sIndex) || p.charAt(pIndex)=='?')){                sIndex++;pIndex++;            }            // 如果字符不同也没有?,但在p中遇到是*时,我们记录下*的位置,但不改变s的指针(匹配空)            else if(pIndex < p.length() && p.charAt(pIndex)=='*'){                starIndex = pIndex;                pIndex++;                sMatchStar = sIndex;//下一个要匹配的还是它            }            // 如果字符不同也没有?,p指向的也不是*,但之前已经遇到*的话,我们可以从idxmatch继续匹配任意字符            else if(starIndex != -1){                pIndex = starIndex+1; //用前一个*来匹配,p的指针也应该退回至上一个*的后面                sMatchStar++; // s匹配*的位置递增,                sIndex = sMatchStar;//s的指针退回到用*匹配的位置            }            else return false;//p的指针前面既没有*,其他又不符合        }        while(pIndex < p.length() && p.charAt(pIndex)=='*'){            pIndex++;        }        return pIndex == p.length();    }}
原创粉丝点击