Skip to content

Commit eadf0e7

Browse files
authored
Create check-if-word-is-valid-after-substitutions.cpp
1 parent a42b99d commit eadf0e7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
bool isValid(string S) {
7+
string stack;
8+
for (const auto& c : S) {
9+
if (c == 'c') {
10+
if (stack.size() >= 2 &&
11+
stack[stack.length() - 2] == 'a' &&
12+
stack[stack.length() - 1] == 'b') {
13+
14+
stack.pop_back();
15+
stack.pop_back();
16+
} else {
17+
return false;
18+
}
19+
} else {
20+
stack.push_back(c);
21+
}
22+
}
23+
return stack.empty();
24+
}
25+
};

0 commit comments

Comments
 (0)