
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)
Editorial
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. |
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.