-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_44.cpp
More file actions
38 lines (34 loc) · 1.17 KB
/
Copy pathleetcode_44.cpp
File metadata and controls
38 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
bool isMatch(string s, string p) {
int sIdx = 0, pIdx = 0;
int starIdx = -1, match = 0;
while (sIdx < s.size()) {
// 문자 일치 또는 '?'인 경우
if (pIdx < p.size() && (p[pIdx] == s[sIdx] || p[pIdx] == '?')) {
sIdx++;
pIdx++;
}
// '*'를 만난 경우
else if (pIdx < p.size() && p[pIdx] == '*') {
starIdx = pIdx;
match = sIdx;
pIdx++;
}
// 이전에 '*'가 있었던 경우, '*'를 통해 한 문자 더 매칭
else if (starIdx != -1) {
pIdx = starIdx + 1;
match++;
sIdx = match;
}
else {
return false;
}
}
// 남은 패턴이 모두 '*'인지 확인
while (pIdx < p.size() && p[pIdx] == '*') {
pIdx++;
}
return pIdx == p.size();
}
};