|
6 | 6 | * cover the entire text ?-> matches single characters *-> match the sequence of
|
7 | 7 | * characters
|
8 | 8 | *
|
9 |
| - * For calculation of Time and Space Complexity. Let N be length of src and M be |
10 |
| - * length of pat |
| 9 | + * For calculation of Time and Space Complexity. Let N be length of src and M be length of pat |
11 | 10 | *
|
| 11 | + * Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/ |
| 12 | + * Question Link : https://practice.geeksforgeeks.org/problems/wildcard-pattern-matching/1 |
12 | 13 | */
|
13 | 14 | public final class RegexMatching {
|
14 | 15 | private RegexMatching() {
|
15 | 16 | }
|
16 | 17 |
|
17 |
| - // Method 1: Using Recursion |
18 |
| - // Time Complexity=0(2^(N+M)) Space Complexity=Recursion Extra Space |
19 |
| - static boolean regexRecursion(String src, String pat) { |
| 18 | + /** |
| 19 | + * Method 1: Determines if the given source string matches the given pattern using a recursive approach. |
| 20 | + * This method directly applies recursion to check if the source string matches the pattern, considering |
| 21 | + * the wildcards '?' and '*'. |
| 22 | + * |
| 23 | + * Time Complexity: O(2^(N+M)), where N is the length of the source string and M is the length of the pattern. |
| 24 | + * Space Complexity: O(N + M) due to the recursion stack. |
| 25 | + * |
| 26 | + * @param src The source string to be matched against the pattern. |
| 27 | + * @param pat The pattern containing wildcards ('*' matches a sequence of characters, '?' matches a single character). |
| 28 | + * @return {@code true} if the source string matches the pattern, {@code false} otherwise. |
| 29 | + */ |
| 30 | + public static boolean regexRecursion(String src, String pat) { |
20 | 31 | if (src.length() == 0 && pat.length() == 0) {
|
21 | 32 | return true;
|
22 | 33 | }
|
@@ -50,8 +61,19 @@ static boolean regexRecursion(String src, String pat) {
|
50 | 61 | return ans;
|
51 | 62 | }
|
52 | 63 |
|
53 |
| - // Method 2: Using Recursion and breaking string using virtual index |
54 |
| - // Time Complexity=0(2^(N+M)) Space Complexity=Recursion Extra Space |
| 64 | + /** |
| 65 | + * Method 2: Determines if the given source string matches the given pattern using recursion. |
| 66 | + * This method utilizes a virtual index for both the source string and the pattern to manage the recursion. |
| 67 | + * |
| 68 | + * Time Complexity: O(2^(N+M)) where N is the length of the source string and M is the length of the pattern. |
| 69 | + * Space Complexity: O(N + M) due to the recursion stack. |
| 70 | + * |
| 71 | + * @param src The source string to be matched against the pattern. |
| 72 | + * @param pat The pattern containing wildcards ('*' matches a sequence of characters, '?' matches a single character). |
| 73 | + * @param svidx The current index in the source string. |
| 74 | + * @param pvidx The current index in the pattern. |
| 75 | + * @return {@code true} if the source string matches the pattern, {@code false} otherwise. |
| 76 | + */ |
55 | 77 | static boolean regexRecursion(String src, String pat, int svidx, int pvidx) {
|
56 | 78 | if (src.length() == svidx && pat.length() == pvidx) {
|
57 | 79 | return true;
|
@@ -83,9 +105,21 @@ static boolean regexRecursion(String src, String pat, int svidx, int pvidx) {
|
83 | 105 | return ans;
|
84 | 106 | }
|
85 | 107 |
|
86 |
| - // Method 3: Top-Down DP(Memoization) |
87 |
| - // Time Complexity=0(N*M) Space Complexity=0(N*M)+Recursion Extra Space |
88 |
| - static boolean regexRecursion(String src, String pat, int svidx, int pvidx, int[][] strg) { |
| 108 | + /** |
| 109 | + * Method 3: Determines if the given source string matches the given pattern using top-down dynamic programming (memoization). |
| 110 | + * This method utilizes memoization to store intermediate results, reducing redundant computations and improving efficiency. |
| 111 | + * |
| 112 | + * Time Complexity: O(N * M), where N is the length of the source string and M is the length of the pattern. |
| 113 | + * Space Complexity: O(N * M) for the memoization table, plus additional space for the recursion stack. |
| 114 | + * |
| 115 | + * @param src The source string to be matched against the pattern. |
| 116 | + * @param pat The pattern containing wildcards ('*' matches a sequence of characters, '?' matches a single character). |
| 117 | + * @param svidx The current index in the source string. |
| 118 | + * @param pvidx The current index in the pattern. |
| 119 | + * @param strg A 2D array used for memoization to store the results of subproblems. |
| 120 | + * @return {@code true} if the source string matches the pattern, {@code false} otherwise. |
| 121 | + */ |
| 122 | + public static boolean regexRecursion(String src, String pat, int svidx, int pvidx, int[][] strg) { |
89 | 123 | if (src.length() == svidx && pat.length() == pvidx) {
|
90 | 124 | return true;
|
91 | 125 | }
|
@@ -120,8 +154,18 @@ static boolean regexRecursion(String src, String pat, int svidx, int pvidx, int[
|
120 | 154 | return ans;
|
121 | 155 | }
|
122 | 156 |
|
123 |
| - // Method 4: Bottom-Up DP(Tabulation) |
124 |
| - // Time Complexity=0(N*M) Space Complexity=0(N*M) |
| 157 | + /** |
| 158 | + * Method 4: Determines if the given source string matches the given pattern using bottom-up dynamic programming (tabulation). |
| 159 | + * This method builds a solution iteratively by filling out a table, where each cell represents whether a substring |
| 160 | + * of the source string matches a substring of the pattern. |
| 161 | + * |
| 162 | + * Time Complexity: O(N * M), where N is the length of the source string and M is the length of the pattern. |
| 163 | + * Space Complexity: O(N * M) for the table used in the tabulation process. |
| 164 | + * |
| 165 | + * @param src The source string to be matched against the pattern. |
| 166 | + * @param pat The pattern containing wildcards ('*' matches a sequence of characters, '?' matches a single character). |
| 167 | + * @return {@code true} if the source string matches the pattern, {@code false} otherwise. |
| 168 | + */ |
125 | 169 | static boolean regexBU(String src, String pat) {
|
126 | 170 | boolean[][] strg = new boolean[src.length() + 1][pat.length() + 1];
|
127 | 171 | strg[src.length()][pat.length()] = true;
|
@@ -153,15 +197,4 @@ static boolean regexBU(String src, String pat) {
|
153 | 197 | }
|
154 | 198 | return strg[0][0];
|
155 | 199 | }
|
156 |
| - |
157 |
| - public static void main(String[] args) { |
158 |
| - String src = "aa"; |
159 |
| - String pat = "*"; |
160 |
| - System.out.println("Method 1: " + regexRecursion(src, pat)); |
161 |
| - System.out.println("Method 2: " + regexRecursion(src, pat, 0, 0)); |
162 |
| - System.out.println("Method 3: " + regexRecursion(src, pat, 0, 0, new int[src.length()][pat.length()])); |
163 |
| - System.out.println("Method 4: " + regexBU(src, pat)); |
164 |
| - } |
165 | 200 | }
|
166 |
| -// Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/ |
167 |
| -// Question Link : https://practice.geeksforgeeks.org/problems/wildcard-pattern-matching/1 |
|
0 commit comments