I am not getting intuition how to solve this problem recursively
I am not getting intuition how to solve this problem recursively
Can you explain a little?
Each occurrence of β?β character in wildcard pattern can be replaced with any other character and each occurrence of β*β with a sequence of characters such that the wildcard pattern becomes identical to the input string after replacement.
Letβs consider any character in the pattern.
Case 1: The character is β* β Here two cases arise
We can ignore β * β character and move to next character in the Pattern. β* β character matches with one or more characters in Text. Here we will move to next character in the string.
Case 2: The character is β?β We can ignore current character in Text and move to next character in the Pattern and Text.
Case 3: The character is not a wildcard character If current character in Text matches with current character in Pattern, we move to next character in the Pattern and Text. If they do not match, wildcard pattern and Text do not match.
I am unable to understand the memoization lines:
1)
if (dp[str.length()][pattern.length()] != 0) {
if (dp[str.length()][pattern.length()] == 1) {
return false;
} else
return true;
}
if (ans) {
dp[str.length()][pattern.length()] = 2;
} else {
dp[str.length()][pattern.length()] = 1;
}
return ans;
these two lines
- if (dp[str.length()][pattern.length()] != 0) {
if (dp[str.length()][pattern.length()] == 1) {
return false;
} else
return true;
}
```Answer: Apply topDown Dp
dp[i] [j] =2// it mens return true
else return false
if (ans) {
dp[str.length()][pattern.length()] = 2; // for true
} else {
dp[str.length()][pattern.length()] = 1;// for false
}
Q2. if (ans) {
dp[str.length()][pattern.length()] = 2;
} else {
dp[str.length()][pattern.length()] = 1;
}
return ans;
these two lines
Answer fill dp array