Tutorialspoint
Problem
Solution
Submissions

Wildcard Matching

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to implement wildcard pattern matching with support for '?' and '*' characters.

  • '?' Matches any single character.
  • '*' Matches any sequence of characters (including an empty sequence).

The matching should cover the entire input string (not partial).

Example 1
  • Input: pattern = "a*c", text = "abc"
  • Output: true
  • Explanation:
    Step 1: The pattern "a*c" needs to match with text "abc".
    Step 2: 'a' matches the first character 'a' in the text.
    Step 3: '*' can match any sequence - in this case it matches 'b'.
    Step 4: 'c' matches the last character 'c' in the text.
    Step 5: The entire text is matched, so return true.
Example 2
  • Input: pattern = "a?c", text = "abcd"
  • Output: false
  • Explanation:
    Step 1: The pattern "a?c" needs to match with text "abcd".
    Step 2: 'a' matches the first character 'a' in the text.
    Step 3: '?' matches any single character - in this case it matches 'b'.
    Step 4: 'c' matches the third character 'c' in the text.
    Step 5: The pattern is completely matched, but there's still 'd' left in the text.
    Step 6: Since the entire text is not matched by the pattern, return false.
Constraints
  • 0 <= pattern.length, text.length <= 1000
  • Pattern and text consist only of lowercase English letters and the wildcard characters '?' and '*'
  • Time Complexity: O(m*n) where m and n are the lengths of the pattern and text respectively
  • Space Complexity: O(m*n)
ArraysDynamic Programming WalmartTutorix
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use dynamic programming to solve this problem.
  • Create a 2D boolean array dp[i][j] which represents if the first i characters of text match the first j characters of pattern.
  • Initialize base cases: empty pattern matches empty string, and handle asterisks in the pattern.
  • For each character pair, update dp[i][j] based on current characters and previous matching states.
  • Handle '?' by checking if the previous characters match.
  • Handle '*' by considering both zero and multiple character matches.

Steps to solve by this approach:

 Step 1: Create a 2D boolean array dp[m+1][n+1] where m is the length of text and n is the length of pattern.

 Step 2: Initialize dp[0][0] as true since empty pattern matches empty text.
 Step 3: Handle the case where pattern starts with "*" characters.
 Step 4: Iterate through each character of text and pattern to fill the dp table.
 Step 5: For each cell, determine match status based on current characters and previous states.
 Step 6: If the current pattern character is "*", it can match zero characters (dp[i][j-1]) or one or more characters (dp[i-1][j]).
 Step 7: If the current pattern character is "?" or matches the text character, the result depends on whether previous substrings match (dp[i-1][j-1]).

Submitted Code :