leetcode 95: Wildcard Matching (uncompleted.)

Wildcard MatchingMar 16 '12

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


public class Solution {
    public boolean isMatch(String s, String p) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(s.length()==0 && p.length()==0) return true;
        if(s.length()==0 ) {
            for(int i=0;i<p.length(); i++) {
                if(p.charAt(i)!='*') return false; 
            }
            return true;
        } else if(p.length()==0 )  {
            return false;
        }
        
        int i=0;
        for(; i<s.length() && i<p.length(); i++) {
            if(s.charAt(i) != p.charAt(i)){
                if(p.charAt(i)=='?') continue;
                else if(p.charAt(i) == '*') {
                    return isMatch( s.substring(i,s.length()), p.substring(i+1,p.length())   ) ||
                           isMatch( s.substring(i+1, s.length()), p.substring(i, p.length())  );
                } else {
                    return false;
                }
            }
        }
        
        return isMatch( s.substring(i,s.length()), p.substring(i,p.length())   );
    }
}


uncompleted

public class Solution {
    public boolean isMatch(String s, String p) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(s==null || p==null) return false;
        
        return matchRec(s, p) ;
    }
    
    private boolean matchRec( String s, String p) {
        int s1 = s.length();
        int p1 = p.length();
        
        if(s1==0 && p1==0) return true;
        else if(p1==0) return false;
        else if(s1==0){
            for(int i=0; i<p1; i++) {
                if(p.charAt(i) != '*') return false;
            }
        } 
        
        int i=0, j=0;
        
        while(i<s1 && j<p1) {
            char c1 = s.charAt(i);
            char c2 = p.charAt(j);
            
            if( c1!=c2 ) {
                if(c2=='?') {
                }
                else if(c2=='*') {
                    return matchRec(s.substring(i+1,s1),p.substring(j,p1)) ||
                            matchRec(s.substring(i,s1),p.substring(j+1,p1));
                } else {
                    return false;
                }
            }
            ++i;++j;
        }
        s = i==s1? new String() : s.substring(i,s1);
        p = j==p1? new String() : p.substring(j,p1);
        return matchRec(s, p);
    }
}


posted @ 2013-03-02 01:41  西施豆腐渣  阅读(141)  评论(0编辑  收藏  举报