Skip to content

Commit 25804e5

Browse files
add 2116
1 parent b83816d commit 25804e5

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2116|[Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2116.java) ||Medium||
1112
|2114|[Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2114.java) ||Easy||
1213
|2110|[Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2110.java) ||Medium||
1314
|2109|[Adding Spaces to a String](https://leetcode.com/problems/adding-spaces-to-a-string/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2109.java) ||Medium||
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2116 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/discuss/1646594/Left-to-right-and-right-to-left
7+
*/
8+
public boolean canBeValid(String s, String locked) {
9+
return s.length() % 2 == 0 && valid(s, locked, '(') && valid(s, locked, ')');
10+
}
11+
12+
private boolean valid(String s, String locked, char op) {
13+
int balance = 0;
14+
int wildcards = 0;
15+
int direction = op == '(' ? 1 : -1;
16+
int start = op == '(' ? 0 : s.length() - 1;
17+
for (int i = start; i < s.length() && i >= 0 && balance + wildcards >= 0; i += direction) {
18+
if (locked.charAt(i) == '1') {
19+
balance += s.charAt(i) == op ? 1 : -1;
20+
} else {
21+
wildcards++;
22+
}
23+
}
24+
return wildcards + balance >= 0 && balance <= wildcards;
25+
}
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2116;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2116Test {
10+
private static _2116.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2116.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.canBeValid("))()))", "010100"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.canBeValid("()()", "0000"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(false, solution1.canBeValid(")", "0"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.canBeValid(")(", "00"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(false, solution1.canBeValid("())(()(()(())()())(())((())(()())((())))))(((((((())(()))))(", "100011110110011011010111100111011101111110000101001101001111"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy