We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a42b99d commit eadf0e7Copy full SHA for eadf0e7
C++/check-if-word-is-valid-after-substitutions.cpp
@@ -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
16
+ } else {
17
+ return false;
18
+ }
19
20
+ stack.push_back(c);
21
22
23
+ return stack.empty();
24
25
+};
0 commit comments