[LeetCode]-- 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

 

 1 public class Solution {
 2    public boolean isMatch(String s, String p) {
 3         int slen = s.length(), plen = p.length();
 4         int i =0, j =0;
 5         int ss =0, starP =- 1;
 6         while(i < slen){
 7             
 8             while( j < plen && p.charAt(j) == '*'){
 9                 starP = j ;
10                 j++;
11                 ss = i;
12             }
13             
14             if(j<plen&&((s.charAt(i) == p.charAt(j) || p.charAt(j) == '?'))){
15                 i++;
16                 j++;
17             }else {
18                 if(starP < 0){
19                     return false;
20                 }else{
21                     j = starP + 1;
22                     i = ++ss;
23                 }
24                 
25             }
26         }
27         
28         
29         while( j < plen && p.charAt(j) == '*'){
30             j++;
31         }
32         
33         return j == plen;
34     }
35 }
View Code

 

posted @ 2014-01-23 14:36  Razer.Lu  阅读(262)  评论(0编辑  收藏  举报