Got Run Error in 2 test cases and 1 wrong answer

Here’s my code - https://ide.codingblocks.com/s/244604

@lousybrick
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.

We can use Dynamic Programming to solve this problem – Let T[i][j] is true if first i characters in given string matches the first j characters of pattern.


// Java Code
public static int isMatch(String src, String pat) {    
        int[][] strg = new int[src.length() + 1][pat.length() + 1];
        for (int row = src.length(); row >= 0; row--) {
            for (int col = pat.length(); col >= 0; col--) {

                if (row == src.length() && col == pat.length()) {
                    strg[row][col] = 1;
                    continue;
                }

                if (row == src.length() && col != pat.length()) {

                    if (pat.charAt(col) == '*') {
                        strg[row][col] = strg[row][col + 1];
                    } else {
                        strg[row][col] = 0;
                    }
                    continue;
                }

                if (row != src.length() && col == pat.length()) {
                    strg[row][col] = 0;
                    continue;
                }

                if (src.charAt(row) == pat.charAt(col) || pat.charAt(col) == '?') {
                    strg[row][col] = strg[row + 1][col + 1];
                } else if (pat.charAt(col) == '*') {
                    boolean Fpart = strg[row][col + 1] == 1;
                    boolean Spart = strg[row + 1][col] == 1;
                    strg[row][col] = ( Fpart|| Spart) ? 1 : 0;
                } else {
                    strg[row][col] = 0;
                }
            }
        }
        return (strg[0][0]);

    }
1 Like

@lousybrick
please your doubt as resolved and rate me as well.

Thank you, also this code gave me a run error for the last test case.

@lousybrick
it will work fine try again.

@lousybrick


refer to this

1 Like