8000 [Wildcard Matching] fix · Pull Request #49 · haoel/leetcode · GitHub
[go: up one dir, main page]

Skip to content

[Wildcard Matching] fix #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Feb 4, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/wildcardMatching/wildcardMatching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,31 @@ using namespace std;


bool isMatch(const char *s, const char *p) {

bool star = false;
const char *s1, *p1;
while( *s && (*p || star) ){
if (*p=='?' || *s == *p){
s++; p++;
}else if (*p=='*'){
//skip the "*", and mark a flag
star = true;
const char *last_s = NULL;
const char *last_p = NULL;
while (*s != '\0') {
// Here, '*' in the pattern is ungreedy. This means we are trying to
// build one or more one-character-to-one-character mappings between
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buggy. * is treated as normal character when it meets *.

// the pattern string and the target string as much as possible.
if (*p == '*') {
last_s = s;
last_p = p++;
} else if (*p == '?' || *p == *s) {
s++;
p++;
//edge case
if (*p=='\0') return true;
//use s1 and p1 to store where the "*" match starts.
s1 = s;
p1 = p;
}else{
if (star==false) return false;
// if meet "*" previously, but the *s != *p
// reset the p, using '*' to match this situation
p = p1;
s = ++s1;
} else if (last_s) {
// Why not use stacks? Because '*' is ungreedy and it can match any
// character. If s becomes '\0' and the rest of p contains
// characters other than '*', that means:
// len(original_s) < len(original_p) - count(original_p, '*')
s = ++last_s;
p = last_p + 1;
} else {
return false;
}
}
//edge case: "s" is done, but "p" still have chars.
if (*s=='\0') {
while (*p=='*') p++; //filter all of '*'
if (*p=='\0') return true;
}
return false;
while (*p == '*') p++;
return *p == '\0';
}


Expand Down Expand Up @@ -89,6 +85,11 @@ int main(int argc, char** argv)
s = "a";
p = "a**";
cout << s << ", " << p << " : " << isMatch(s, p) << endl;

s = "*aa";
p = "*a";
cout << s << ", " << p << " : " << isMatch(s, p) << endl;

if (argc>2){
s = argv[1];
p = argv[2];
Expand Down
0