码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-44 Wildcard Matching

时间:2015-03-18 17:20:10      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

‘?‘ 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

思路:使用动态动态规划的解法
设dp[i][j]表示s[0,i]与p[0,j]是否match;
代码如下:这种方法在运行时内存溢出了
public boolean isMatch(String s, String p){
        if(s != null && p != null && s.equals(p))
            return true;
        if("".equals(s) || "".equals(p))
            return false;
        int n = s.length();
        int m = p.length();
        boolean[][] dp = new boolean[n][m];
        
        if(s.charAt(0) == p.charAt(0) || p.charAt(0) == ‘?‘ || p.charAt(0) == ‘*‘)
            dp[0][0] = true;
        else
            return false;
        
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(i == 0 && j > 0) {
                    if(p.charAt(j) == ‘*‘)
                        dp[i][j] = dp[i][j-1];
                    else
                        dp[i][j] = false;
                }else if(i > 0 && j == 0) {
                    if(p.charAt(j) == ‘*‘)
                        dp[i][j] = true;
                    else 
                        dp[i][j] = false;
                } else if(i>0 && j>0){
                    if(p.charAt(j) == ‘*‘) {
                        dp[i][j] = (dp[i-1][j] || dp[i][j-1]); 
                    } else if(p.charAt(j) == ‘?‘ || s.charAt(i) == p.charAt(j)) {
                        dp[i][j] = dp[i-1][j-1];
                    } else {
                        return false;
                    }
                }
            }
        }
        return dp[n-1][m-1];
    }

 

使用循环遍历两个字符串,代码如下

public boolean isMatch(String s, String p) {
        if("*".equals(p))
            return true;
        
        int posS = 0;
        int posP = 0;
        int posG = -1;//最近一个“*”的位置
        int posSnext = -1;
        while(posS < s.length()) {
            if(posP<p.length() && p.charAt(posP) == ‘*‘) {
                posG = posP;
                posP++;
                posSnext = posS + 1;
            } else if(posP<p.length() && (s.charAt(posS) == p.charAt(posP) || p.charAt(posP) == ‘?‘)) {
                posS++;
                posP++;
            } else if(posG == -1){
                return false;
            } else {
                posP = posG+1;
                posS = posSnext;
                posSnext++;
            }
        }
        
        while(posP < p.length() && p.charAt(posP) == ‘*‘)
            posP++;
        if(posP >= p.length())
            return true;
        return false;
    }

 

LeetCode-44 Wildcard Matching

标签:

原文地址:http://www.cnblogs.com/linxiong/p/4347718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!