Skip to content

Commit 180b195

Browse files
committed
Problem 173: Wildcard matching problem
1 parent cd1585b commit 180b195

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 172 |
9+
| Total Problems | 173 |
1010

1111
</center>
1212

@@ -210,6 +210,8 @@ Include contains single header implementation of data structures and some algori
210210
| Problem | Solution |
211211
| :------------ | :----------: |
212212
| You are given a digit string (e.g "1234", "567" etc), provide all possible letter combinations we could generate from this digit string, based on the mapping we see on the telphone/mobile dialpad. If you have typed SMS in old style phones, you would know. For e.g. "1" is mapped to "abc", 2 is mapped to "def". You can refer to the <a href="http://techotv.com/wp-content/uploads/2013/03/Lumia-620-dial-pad-screen-1.jpg" target="_blank">image.</a>. <ul><li> Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"} </li></ul> Note that order does not matter in result set.|[dialpad_combinations.cpp](backtracking_problems/dialpad_combinations.cpp)|
213+
| Implement wildcard pattern maching with support for '?' & '*'. <ul><li>'?' Matches any single character.</li></ul><ul><li>'*' Matches any sequence of character.</li></ul>. Checkout examples in file for more details.|[wild_card_matching.cpp](backtracking_problems/wild_card_matching.cpp)|
214+
213215

214216

215217
### Leet code Problems
@@ -248,4 +250,3 @@ Include contains single header implementation of data structures and some algori
248250
| Count the number of islands in a grid. Given a grid representing 1 as land body, and 0 as water body, determine the number of islands (more details in problem comments)|[count_islands.cpp](leet_code_problems/count_islands.cpp)|
249251
| Find median from a data stream. Design a data structure that supports addNum to add a number to the stream, and findMedian to return the median of the current numbers seen so far. Also, if the count of numbers is even, return average of two middle elements, return median otherwise.|[median_stream.cpp](leet_code_problems/median_stream.cpp)
250252
| Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ) | [remove_invalid_parenthesis.cpp](leet_code_problems/remove_invalid_parenthesis.cpp)|
251-
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Implement wildcard pattern maching with support for '?' & '*'
3+
* '?' Matches any single character
4+
* '*' Matches any sequence of character
5+
* Source : Leetcode problem 44.
6+
*
7+
* Approach:
8+
*
9+
* Consider examples:
10+
* is_match("aa", "a") false
11+
* is_match("aa", "aaa") false
12+
* is_match("aa", "?a") true
13+
* is_match("aa", "?*") true
14+
* is_match("aa", "*") true
15+
* is_match("aa", "*a") true
16+
* is_match("aa", "a*") true
17+
* is_match("cabcab", "*ab") true
18+
*
19+
* Approach:
20+
* Clearly, the '*' with multiple pattern matches makes this problem
21+
* a little complicated, for example last case in the example.
22+
* The second pattern of ab will give us the right answer.
23+
* Therefore, we will have to try and match the same * with multiple
24+
* patterns, and backtrack when we fail to match.
25+
* For example:
26+
* When we iterate source string and target pattern
27+
* "cabcab" and "*ab" with i and j respectively, we will realize at
28+
* i = 3 and j = 3 that we failed, at that time, we will have backtrack
29+
* j back to position next to '*' i.e. j = 1, so that we can continue
30+
* looking for further occurances of ab.
31+
* Therefore, we will have to remember positions of '*' and reset
32+
* iterator when we try to match the pattern.
33+
*/
34+
35+
#include <iostream>
36+
#include <string>
37+
38+
39+
bool is_match(const std::string& str, const std::string& pattern)
40+
{
41+
int sLen = str.length();
42+
int pLen = pattern.length();
43+
int i = 0;
44+
int j = 0;
45+
int last_star_position = -1;
46+
int last_match = -1;
47+
while (i < sLen) {
48+
if (j < pLen && (str[i] == pattern[j] ||
49+
pattern[j] == '?')) {
50+
++i;
51+
++j;
52+
}
53+
else if (j < pLen && pattern[j] == '*') {
54+
last_star_position = j;
55+
last_match = i;
56+
j++;
57+
}
58+
else if (last_star_position != -1) {
59+
j = last_star_position + 1;
60+
last_match++;
61+
i = last_match;
62+
}
63+
else return false;
64+
}
65+
66+
while (j < pLen && pattern[j] == '*') {
67+
++j;
68+
}
69+
return j == pLen;
70+
}
71+
72+
int main()
73+
{
74+
std::string str{"cabcab"};
75+
std::string pattern{"*ab"};
76+
if (is_match(str, pattern)) {
77+
std::cout << "'" << str << "'"
78+
<< " and '" << pattern << "'" << " are a match\n";
79+
} else {
80+
std::cout << "'" << str << "'"
81+
<< " and '" << pattern << "'" << " are not a match\n";
82+
}
83+
return 0;
84+
}

0 commit comments

Comments
 (0)