aboutsummaryrefslogtreecommitdiff
path: root/wildcard_matching.cpp
blob: 23a6538a071279b040f7c2e03090d96c4217c848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "leetcode.h"

class Solution {
public:
  bool isMatch(string s, string p) {
    int m = s.length(), n = p.length();
    int i = 0, j = 0, astr = -1, match;
    while (i < m) {
      if (j < n && p[j] == '*')
        match = i, astr = j++;
      else if (j < n && (s[i] == p[j] || p[j] == '?'))
        i++, j++;
      else if (astr >= 0)
        i = ++match, j = astr + 1;
      else
        return false;
    }
    while (j < n && p[j] == '*')
      j++;
    return j == n;
  }
};

int main() {}