java 44.通配符匹配(#)。java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 44.通配符匹配(#)。java相关的知识,希望对你有一定的参考价值。
public class Solution {
public boolean isMatch(String s, String p) {
return helper(s.toCharArray(), p.toCharArray(), 0, 0, 0, -1);
}
private boolean helper(char[] s, char[] p, int i, int j, int lastS, int lastP) {
if(i == s.length && j == p.length) return true;
if(j < p.length) {
if(i == s.length)
return p[j] == '*' ? helper(s, p, i, j + 1, lastS, lastP) : false;
if(p[j] == '?' || s[i] == p[j])
return helper(s, p, i + 1, j + 1, lastS, lastP);
if(p[j] == '*')
return helper(s, p, i, j + 1, i, j);
}
if(lastP != -1) return helper(s, p, lastS + 1, lastP, lastS + 1, lastP);
return false;
}
}
public class Solution {
public boolean isMatch(String str, String pattern) {
if(str == null || pattern == null) return false;
boolean[][] dp = new boolean[str.length() + 1][pattern.length() + 1];
dp[0][0] = true;
for(int i = 1; i <= pattern.length(); i++) {
if(pattern.charAt(i - 1) == '*') {
dp[0][i] = dp[0][i - 1];
} else {
break;
}
}
for(int i = 1; i < str.length() + 1; i++){
for(int j = 1; j < pattern.length() + 1; j++) {
if(pattern.charAt(j - 1) == '?' || pattern.charAt(j - 1) == str.charAt(i - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else if(pattern.charAt(j - 1) == '*') {
dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i - 1][j - 1];
}
}
}
return dp[str.length()][pattern.length()];
// The complexity of the algorithm is O(p*s),
//worst case
//s = "aaaaaaaaaaaaaaaaaaaa"
//p = "*aaaaaa"
// int s = 0, p = 0, match = 0, starIdx = -1;
// while (s < str.length()){
// // advancing both pointers
// if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
// s++;
// p++;
// }
// // * found, only advancing pattern pointer
// else if (p < pattern.length() && pattern.charAt(p) == '*'){
// starIdx = p;
// match = s;
// p++;
// }
// // last pattern pointer was *, advancing string pointer
// else if (starIdx != -1){
// p = starIdx + 1;
// match++;
// s = match;
// }
// //current pattern pointer is not star, last patter pointer was not *
// //characters do not match
// else return false;
// }
// //check for remaining characters in pattern
// while (p < pattern.length() && pattern.charAt(p) == '*')
// p++;
// return p == pattern.length();
//recursive
//public class Solution {
// public boolean isMatch(String s, String p) {
// return helper(s,p,0,0);
// }
// boolean helper(String s, String p, int l, int r) {
// if(r == p.length()) return l == s.length();
// if(p.charAt(r) == '*') {
// while(r < p.length() && p.charAt(r) == '*') r++; // Move the index at p to a non-start char.
// while(l < s.length()) {
// if(helper(s, p, l, r)) return true; // Find one match, return true.
// l++; // Try the next one.
// }
// return helper(s, p, l, r);
// }else if(l < s.length() && (p.charAt(r) == '?' || s.charAt(l) == p.charAt(r))){
// return helper(s, p, l + 1, r + 1);
// }else{
// return false;
// }
// }
// }
}
}
public class Solution {
public boolean isMatch(String str, String pattern) {
int s = 0, p = 0, match = 0, starIdx = -1;
while (s < str.length()){
// advancing both pointers
if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
s++;
p++;
}
// * found, only advancing pattern pointer
else if (p < pattern.length() && pattern.charAt(p) == '*'){
starIdx = p;
match = s;
p++;
}
// last pattern pointer was *, advancing string pointer
else if (starIdx != -1){
p = starIdx + 1;
match++;
s = match;
}
//current pattern pointer is not star, last patter pointer was not *
//characters do not match
else return false;
}
//check for remaining characters in pattern
while (p < pattern.length() && pattern.charAt(p) == '*')
p++;
return p == pattern.length();
}
}
以上是关于java 44.通配符匹配(#)。java的主要内容,如果未能解决你的问题,请参考以下文章