Fork me on GitHub

LeetCode 44 Wildcard Matching(字符串匹配问题)

 
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

字符串匹配问题。
如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串
 给定字符串s,和字符串p。判断按照以上规则,字符串p是否能够匹配字符串s
 
 
参考代码: 
package leetcode_50;


/***
 * 
 * @author pengfei_zheng
 * 字符串匹配问题
 */
public class Solution44 {
    public boolean isMatch(String s, String p) {
        int sp = 0, pp = 0, match = 0, starIdx = -1;            
        while (sp < s.length()){
            if (pp < p.length()  && (p.charAt(pp) == '?' 
                    || s.charAt(sp) == p.charAt(pp))){
                sp++;
                pp++;
            }
            else if (pp < p.length() && p.charAt(pp) == '*'){
                starIdx = pp;
                match = sp;
                pp++;
            }
            else if (starIdx != -1){
                pp = starIdx + 1;
                match++;
                sp = match;
            }
            else return false;
        }
        while (pp < p.length() && p.charAt(pp) == '*')
            pp++;
        
        return pp == p.length();
    }
}

 

 
 
posted @ 2017-03-13 12:15  伊甸一点  阅读(751)  评论(0编辑  收藏  举报