Skip to content

Commit

Permalink
Problem 173: Wildcard matching problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jan 15, 2018
1 parent cd1585b commit 180b195
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 172 |
| Total Problems | 173 |

</center>

Expand Down Expand Up @@ -210,6 +210,8 @@ Include contains single header implementation of data structures and some algori
| Problem | Solution |
| :------------ | :----------: |
| 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)|
| 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)|



### Leet code Problems
Expand Down Expand Up @@ -248,4 +250,3 @@ Include contains single header implementation of data structures and some algori
| 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)|
| 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)
| 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)|

84 changes: 84 additions & 0 deletions backtracking_problems/wild_card_matching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Implement wildcard pattern maching with support for '?' & '*'
* '?' Matches any single character
* '*' Matches any sequence of character
* Source : Leetcode problem 44.
*
* Approach:
*
* Consider examples:
* is_match("aa", "a") false
* is_match("aa", "aaa") false
* is_match("aa", "?a") true
* is_match("aa", "?*") true
* is_match("aa", "*") true
* is_match("aa", "*a") true
* is_match("aa", "a*") true
* is_match("cabcab", "*ab") true
*
* Approach:
* Clearly, the '*' with multiple pattern matches makes this problem
* a little complicated, for example last case in the example.
* The second pattern of ab will give us the right answer.
* Therefore, we will have to try and match the same * with multiple
* patterns, and backtrack when we fail to match.
* For example:
* When we iterate source string and target pattern
* "cabcab" and "*ab" with i and j respectively, we will realize at
* i = 3 and j = 3 that we failed, at that time, we will have backtrack
* j back to position next to '*' i.e. j = 1, so that we can continue
* looking for further occurances of ab.
* Therefore, we will have to remember positions of '*' and reset
* iterator when we try to match the pattern.
*/

#include <iostream>
#include <string>


bool is_match(const std::string& str, const std::string& pattern)
{
int sLen = str.length();
int pLen = pattern.length();
int i = 0;
int j = 0;
int last_star_position = -1;
int last_match = -1;
while (i < sLen) {
if (j < pLen && (str[i] == pattern[j] ||
pattern[j] == '?')) {
++i;
++j;
}
else if (j < pLen && pattern[j] == '*') {
last_star_position = j;
last_match = i;
j++;
}
else if (last_star_position != -1) {
j = last_star_position + 1;
last_match++;
i = last_match;
}
else return false;
}

while (j < pLen && pattern[j] == '*') {
++j;
}
return j == pLen;
}

int main()
{
std::string str{"cabcab"};
std::string pattern{"*ab"};
if (is_match(str, pattern)) {
std::cout << "'" << str << "'"
<< " and '" << pattern << "'" << " are a match\n";
} else {
std::cout << "'" << str << "'"
<< " and '" << pattern << "'" << " are not a match\n";
}
return 0;
}

0 comments on commit 180b195

Please sign in to comment.