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 c8ba835 commit 9d40369Copy full SHA for 9d40369
Medium/Check if a Parentheses String Can Be Valid.java
@@ -0,0 +1,30 @@
1
+class Solution {
2
+ public boolean canBeValid(String s, String locked) {
3
+ if (s.length() % 2 != 0) {
4
+ return false;
5
+ }
6
+ Stack<Integer> openBrackets = new Stack<>();
7
+ Stack<Integer> unlocked = new Stack<>();
8
+ for (int i = 0; i < s.length(); i++) {
9
+ char c = s.charAt(i);
10
+ if (locked.charAt(i) == '0') {
11
+ unlocked.push(i);
12
+ } else if (c == '(') {
13
+ openBrackets.push(i);
14
+ } else if (c == ')') {
15
+ if (!openBrackets.isEmpty()) {
16
+ openBrackets.pop();
17
+ } else if (!unlocked.isEmpty()) {
18
+ unlocked.pop();
19
+ } else {
20
21
22
23
24
+ while (!openBrackets.isEmpty() && !unlocked.isEmpty() && openBrackets.peek() < unlocked.peek()) {
25
26
27
28
+ return openBrackets.isEmpty();
29
30
+}
0 commit comments