-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 15 ms (83.4%), Space: 45.5 MB (87.55%) - LeetHub
- Loading branch information
1 parent
9ff7ebf
commit fdc7d70
Showing
1 changed file
with
17 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...if-a-parentheses-string-can-be-valid/2116-check-if-a-parentheses-string-can-be-valid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public boolean canBeValid(String s, String locked) { | ||
int n = s.length(); | ||
if (n % 2 != 0) return false; | ||
int min = 0, max = 0; | ||
for (int i = 0; i < n; i++) { | ||
boolean open = s.charAt(i) == '('; | ||
boolean unlocked = locked.charAt(i) == '0'; | ||
min += (!open || unlocked) ? -1 : 1; | ||
max += (open || unlocked) ? 1 : -1; | ||
|
||
if (max < 0) return false; | ||
min = Math.max(min, 0); | ||
} | ||
return min == 0; | ||
} | ||
} |