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 WildcardMatching {
public boolean subMatch(String s, String p,int iStart,int jStart) throws InterruptedException{
int i = iStart;
int j = jStart;
int lens = s.length();
int lenp = p.length();
System.out.println("\n\nrecursion ====== "+lens+" "+lenp);
boolean res = false;
while(j j++, and break;(no need to recursion)
* if next is ? => i++(start index skep 1),j++;
*/
if(j=lens-1){//? match the last one in s
while(j=lenp){
res = true;
return res;
}
}
continue; //go to loop again
}else{
//go to recursion branch
while(i= lenp; one/multiple * in the tail of p
res = true;
return res;
}
} //end if(*)
if(i=lenp-1){
res = true;
}else{
res = false;
}
break;
}else if(i==lens-1 && j==lenp-1){//both get the end
if(s.charAt(i)==p.charAt(j) || p.charAt(j) == '?'){
res = true;
return res;
}
}
if(s.charAt(i)==p.charAt(j)||p.charAt(j)=='?'){ //loop to compare
i++;
j++;
}else{
res = false;
break;
}
}
return res;
}
public boolean isMatch(String s, String p) throws InterruptedException{
boolean res = subMatch(s,p,0,0);
return res;
}
public static void main(String[] args) throws InterruptedException{
// String s = "abcdefga";
// String p = "ab?*g*a";
// String s = "abcdefga";
// String p = "ab?*g*";
// String s = "aaaba";
// String p = "a*?a*b*a";
// String p = "aaa***ba";
// String s = "abab";
// String p = "*?*ba*?*";
// String p = "***ab***";
// String s = "abc12abc1d1dabc1d1e";
// String p = "*abc?d?*e";
String s = "abab1";
String p = "*?*ba*??*";
// String s = "ab";
// String p = "??";
WildcardMatching wm = new WildcardMatching();
System.out.println(s+"\n"+p+"\n");
boolean res = wm.isMatch(s,p);
System.out.println("\nres: "+res);
}
}
---------------------------------------------------------------------------------------------------
本文链接:http://blog.csdn.NET/karen0310/article/details/75053593
请尊重作者的劳动成果,转载请注明出处!
---------------------------------------------------------------------------------------------------