[InterviewBit] Regular Expression Match

Regular Expression Match

  • Time : O(nm)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int Solution::isMatch(const string A, const string B) {
int n = A.length(), m = B.length();
int i = 0, j = 0, si = -1, sj = -1;
while(i < n) {
if(A[i] == B[j] or B[j] == '?') {
i++;
j++;
} else if(B[j] == '*') {
si = i;
sj = j;
j++;
} else if(sj != -1) {
i = si + 1;
j = sj + 1;
si++;
} else return 0;
}

while(j < m and B[j] == '*') j++;
return j == m;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/29/PS/interviewbit/regular-expression-match/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.