Easy DP Java Solution with detailed Explanation

• This Solution use 2D DP. beat 90% solutions, very simple.

Here are some conditions to figure out, then the logic can be very straightforward.

``````1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
3, If p.charAt(j) == '*':
here are two sub conditions:
1   if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]  //in this case, a* only counts as empty
2   if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':
dp[i][j] = dp[i-1][j]    //in this case, a* counts as multiple a
or dp[i][j] = dp[i][j-1]   // in this case, a* counts as single a
or dp[i][j] = dp[i][j-2]   // in this case, a* counts as empty
``````

Here is the solution

``````public boolean isMatch(String s, String p) {

if (s == null || p == null) {
return false;
}
boolean[][] dp = new boolean[s.length()+1][p.length()+1];
dp[0][0] = true;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*' && dp[0][i-1]) {
dp[0][i+1] = true;
}
}
for (int i = 0 ; i < s.length(); i++) {
for (int j = 0; j < p.length(); j++) {
if (p.charAt(j) == '.') {
dp[i+1][j+1] = dp[i][j];
}
if (p.charAt(j) == s.charAt(i)) {
dp[i+1][j+1] = dp[i][j];
}
if (p.charAt(j) == '*') {
if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.') {
dp[i+1][j+1] = dp[i+1][j-1];
} else {
dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]);
}
}
}
}
return dp[s.length()][p.length()];
}``````

• Your logic is very concise! Nice explanation.

• Line 9: java.lang.ArrayIndexOutOfBoundsException: -1

• I re-runs the code, it works well. When you created 2D array, its length should be s.length()+1. I think you may make a mistake here.

• so true when i == 0, dp[0][i - 1] definitely throws out an exception

• Hi,
in your pseudo code, in line 6.
"p.charAt(i-1)" may give out of bound .. should it be j-1 ?

• Hi, have you tried "a" ".**" ,it doesn't accord with the expection

• for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '' && dp[0][i-1]) {
dp[0][i+1] = true;
}
}
While, before that code, we need to add
if(p.charAt(0)=='
')
dp[0][1]=true;

then the pointer i should begin with 1,
for(int i=1; i<p.length(); i++){
...........

}

• Most clearly solution I have seen

• Great explanation! But it seems that the solution code cannot be accepted. I revised your code:

``````public class Solution {
public boolean isMatch(String s, String p) {
if(s == null || p == null) {
return false;
}
boolean[][] state = new boolean[s.length() + 1][p.length() + 1];
state[0][0] = true;
// no need to initialize state[i][0] as false
// initialize state[0][j]
for (int j = 1; j < state[0].length; j++) {
if (p.charAt(j - 1) == '*') {
if (state[0][j - 1] || (j > 1 && state[0][j - 2])) {
state[0][j] = true;
}
}
}
for (int i = 1; i < state.length; i++) {
for (int j = 1; j < state[0].length; j++) {
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
state[i][j] = state[i - 1][j - 1];
}
if (p.charAt(j - 1) == '*') {
if (s.charAt(i - 1) != p.charAt(j - 2) && p.charAt(j - 2) != '.') {
state[i][j] = state[i][j - 2];
} else {
state[i][j] = state[i - 1][j] || state[i][j - 1] || state[i][j - 2];
}
}
}
}
return state[s.length()][p.length()];
}
}
``````

• @hongchang It won't reach that statement as there is one hidden assumption of the problem "the first character cannot be '*'".

• @perfecthu Hi Perfecthu, I think in this problem the hidden assumption is * cannot be the first character.

• What if the Start of p is *, the for (int i = 0; i < p.length(); i++) should be changed to i = 1?

• @SonglianLi The start of p won't be "p" because of the hidden assumption of this problem. If there is one '*', it must follow some other character. It is not hard to find this assumption if you read the problem again.

• This post is deleted!

• @dolphinwby The code in the top post is accepted when I submitted it. And seems your solution is almost the same as the top post, but it runs faster... don't know why

• `r[i][j]` means to index `i` of `s` and index `j` of `p`, whether it match or not.
`if(pc[j - 1] == "*") r[0][j] = r[0][j - 2]` why `j - 2` here, means `*` cannot work alone.

``````  public boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
char[] sc = s.toCharArray(), pc = p.toCharArray();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for(int i = 2; i <= n; i++){
if(pc[i - 1] == '*'){
dp[0][i] = dp[0][i - 2]; // *可以消掉c*
}
}

for(int i = 1; i <= m; i ++){
for(int j = 1; j <= n; j++){
if(sc[i - 1] == pc[j - 1] || pc[j - 1] == '.'){
dp[i][j] = dp[i - 1][j - 1];
} else if(pc[j - 1] == '*'){
if(sc[i - 1] == pc[j - 2] || pc[j - 2] == '.'){
dp[i][j] = dp[i][j - 2] || dp[i][j - 1] || dp[i - 1][j];
// 当*的前一位是'.'， 或者前一位的pc等于sc的话，
// *代表1个(dp[i][j - 1])，*代表多个(dp[i - 1][j])，或者用*消掉c*(dp[i][j - 2])
} else {
dp[i][j] = dp[i][j - 2]; // 用*消掉c*
}
} else {
dp[i][j] = false;
}
}
}

return dp[m][n];
}
``````

• 2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':

very nice explanation and implementation. I found in the line " 2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':", all the 'p.charAt(i ' should be replaced to "p.charAt( j ". please correct me if I was wrong

• Why if there's multiple same chars, dp[i][j] = dp[i-1][j]?

• @Duor Because dp is increasing from first letter to last, so if there are multiple same letters, it is still counting from first repeated to last, so the first time when we see * and the first repeated letter, we will use as single letter case, and next when we see the second letter, we will simple check if there previous is single letter case and if that is true. if the single letter matches *, it means the second is also true. And if we check the third, forth and so on it just need to take the previous results.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.