题目:
Pattern Matching, if '.' is used as a wildcard, which means '.'can represent any character.
注释:
这道题题目给的不清楚,我的代码是假设:给定两个字符串,只有第二个字符串包含'.'。
Code:
import java.util.*;
class Test{
public static void main(String[] args){
Solution sol = new Solution();
String str1 = "q.abc";
String str2 = "a.c.";
System.out.println(sol.isMatch(str1, str2));
}
}
class Solution{
public boolean isMatch(String str1, String str2){
if(str1.length()==0){
if(str2.length()==0) return true;
if(str2.charAt(0)=='.') return isMatch(str1, str2.substring(1));
else return false;
}
if(str2.length()==0) return str1.length()==0;
if(str2.charAt(0)!='.'){
if(str1.charAt(0)==str2.charAt(0)) return isMatch(str1.substring(1), str2.substring(1));
else return false;
}
else{
return isMatch(str1.substring(1), str2.substring(1)) || isMatch(str1, str2.substring(1));
}
}
}
小结:
Follow up:如果第一个字符串也可以包含'.'呢?