LeetCode刷題記126-44. 萬用字元匹配
LeetCode刷題記126
44. 萬用字元匹配
class Solution {
public boolean isMatch(String s, String p) {
if (s.length() == 0) {
if (p.length() == 0) return true;
for (int i = 0; i < p.length(); i ++) {
if (p.charAt(i) != '*') return false;
}
return true;
}
if (p.length() == 0) {
return false;
}
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true;
for (int j = 0; j < p.length(); j ++) {
if (p.charAt(j) != '*') break;
dp[0][j + 1] = true;
}
for (int i = 0; i < s.length(); i ++) {
char a = s.charAt(i);
for (int j = 0; j < p.length(); j ++) {
char b = p.charAt(j);
if (a == b || b == '?') {
dp[i + 1][j + 1] |= dp[i][j];
} else {
if (b == '*') {
dp[i + 1][j + 1] |= dp[i + 1][j];
dp[i + 1][j + 1] |= dp[i][j + 1];
}
}
// System.out.println("i " + i + " j "+ j +" " + dp[i+1][j+1]);
}
}
return dp[s.length()][p.length()];
}
}
// ""
// "***"
//True
2/3
126/150
相關文章
- LeetCode 44 萬用字元匹配LeetCode字元
- LeetCode 刷題筆記LeetCode筆記
- LeetCode刷題記錄LeetCode
- leetcode刷題筆記LeetCode筆記
- leetcode刷題記錄 661~LeetCode
- leetcode刷題筆記605LeetCode筆記
- leetcode刷題筆記(3)(python)LeetCode筆記Python
- Python3 - 用Shell萬用字元匹配字串Python字元字串
- 用 Rust 刷 leetcode 第七題RustLeetCode
- 用 Rust 刷 leetcode 第一題RustLeetCode
- 用 Rust 刷 leetcode 第三題RustLeetCode
- 用 Rust 刷 leetcode 第二題RustLeetCode
- 用Rust刷leetcode第九題RustLeetCode
- 用Rust刷leetcode第八題RustLeetCode
- 用 Rust 刷 leetcode 第六題RustLeetCode
- 用 Rust 刷 leetcode 第五題RustLeetCode
- 用 Rust 刷 leetcode 第四題RustLeetCode
- 用Rust刷leetcode第十題RustLeetCode
- LeetCode刷題筆記9.2-9.9LeetCode筆記
- leetcode刷題筆記8.5-8.9LeetCode筆記
- Leetcode刷題筆記8.12-8.16LeetCode筆記
- LeetCode刷題記錄——day1LeetCode
- LeetCode刷題記錄——day5LeetCode
- LeetCode刷題記錄——day4LeetCode
- LeetCode刷題記錄——day3LeetCode
- LeetCode刷題記錄——day2LeetCode
- 用Rust刷leetcode第十三題RustLeetCode
- 用Rust刷leetcode第十四題RustLeetCode
- 用Rust刷leetcode第十七題RustLeetCode
- 用Rust刷leetcode第十五題RustLeetCode
- 用Rust刷leetcode第十六題RustLeetCode
- 用Rust刷leetcode第十一題RustLeetCode
- 用Rust刷leetcode第十二題RustLeetCode
- LeetCode刷題記13-27. 移除元素LeetCode
- LeetCode-劍指Offer刷題記錄LeetCode
- LeetCode 刷題—樹LeetCode
- LeetCode刷題 堆LeetCode
- LeetCode刷題整理LeetCode