前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LintCode 通配符匹配分析

LintCode 通配符匹配分析

作者头像
desperate633
发布2018-08-22 15:24:20
3260
发布2018-08-22 15:24:20
举报
文章被收录于专栏:desperate633desperate633

判断两个可能包含通配符“?”和“*”的字符串是否匹配。匹配规则如下:

'?' 可以匹配任何单个字符。 '*' 可以匹配任意字符串(包括空字符串)。

两个串完全匹配才算匹配成功。

函数接口如下: bool isMatch(const char *s, const char *p)

样例 一些例子:

isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "") → true isMatch("aa", "a") → true isMatch("ab", "?") → true isMatch("aab", "ca*b") → false

分析

方法一:

动态规划: match[i][j] : 表示从i到s.length,从j到p.length的是否匹配 状态转移方程: 如果 s[i]==p[j], 那么自然,match[i][j] = match[i+1][j+1]; 如果 p[j] == '?' 自然,match[i][j] = match[i+1][j+1]; 如果p[j] == '' 分三种情况, 只匹配s[i] 那么,match[i][j] = [i+1][j+1]; *作为空值出现 那么,macth[i][j] = match[i][j+1] *匹配两个或者以上字符 那么,match[i][j] = match[i+1][j]

初始化: 如果p的后面有连续字符为*时,可以初始化为true。

代码语言:javascript
复制
public class Solution {
    /**
     * @param s: A string 
     * @param p: A string includes "?" and "*"
     * @return: A boolean
     */
    public boolean isMatch(String s, String p) {
       boolean[][] match=new boolean[s.length()+1][p.length()+1];
        match[s.length()][p.length()]=true;
        
        
        
        for(int i=p.length()-1;i>=0;i--){
            if(p.charAt(i)!='*')
                break;
            else
                match[s.length()][i]=true;
        }
        for(int i=s.length()-1;i>=0;i--){
            for(int j=p.length()-1;j>=0;j--){
                if(s.charAt(i)==p.charAt(j)||p.charAt(j)=='?')
                        match[i][j]=match[i+1][j+1];
                else if(p.charAt(j)=='*')
                        match[i][j]=match[i+1][j]||match[i][j+1] || match[i+1][j+1];
                else
                    match[i][j]=false;
            }
        }
        return match[0][0];
     }
}

/*aab  s 

a**b   p

m[i][j]  

m[2][3] = m[3][4]
m[2][2] = if * == b:m[3][3]
            if * == empty m[2][3]
            if * == 其他 m[3][2]
            
*/

方法二

两根指针遍历,记录*出现的位置

代码语言:javascript
复制
public class WildcardMatching {
    public boolean isMatch(String str, String pattern) {
           int s = 0;
           int p = 0;
           int sMatch = -1;//*匹配s中的字符的终止位置
           int starIdx = -1;//最近一个*出现的位置
           while (s < str.length()){
                // advancing both pointers
                if (p < pattern.length()  && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
                    s++;
                    p++;
                }
                // * found, only advancing pattern pointer,,先让* 作为empty出现,所以s不需要++
                else if (p < pattern.length() && pattern.charAt(p) == '*'){
                    starIdx = p;//record the index of *
                    sMatch = s;//record the * 匹配的s中的位置
                    p++;
                }
               //last pattern pointer was *, advancing string pointer,用*去匹配当前的串
                else if (starIdx != -1){
                    p = starIdx + 1;//只能用* 去匹配,所以p要回到*后面一个元素开始判断
                    sMatch++;//被*匹配了
                    s = sMatch;
                }
               //current pattern pointer is not star, last patter pointer was not *
              //characters do not match
                else return false;
            }
            
            //check for remaining characters in pattern
            while (p < pattern.length() && pattern.charAt(p) == '*')
                p++;
            
            return p == pattern.length();
         }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分析
    • 方法一:
      • 方法二
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档