LeetCode 2116. Check if a Parentheses String Can Be Valid

原题链接在这里:https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/description/
题目:

A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:

  • It is ().
  • It can be written as AB (A concatenated with B), where A and B are valid parentheses strings.
  • It can be written as (A), where A is a valid parentheses string.

You are given a parentheses string s and a string locked, both of length nlocked is a binary string consisting only of '0's and '1's. For each index i of locked,

  • If locked[i] is '1', you cannot change s[i].
  • But if locked[i] is '0', you can change s[i] to either '(' or ')'.

Return true if you can make s a valid parentheses string. Otherwise, return false.

Example 1:

Input: s = "))()))", locked = "010100"
Output: true
Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.

Example 2:

Input: s = "()()", locked = "0000"
Output: true
Explanation: We do not need to make any changes because s is already valid.

Example 3:

Input: s = ")", locked = "0"
Output: false
Explanation: locked permits us to change s[0]. 
Changing s[0] to either '(' or ')' will not make s valid.

Constraints:

  • n == s.length == locked.length
  • 1 <= n <= 105
  • s[i] is either '(' or ')'.
  • locked[i] is either '0' or '1'.

题解:

For string parenthese question, try to think as it could be done with left to right, then rigth to left approach.

When iterate from left to right, we have flipCount and lockCount. When locked char is '0', increment flipCount. This could be used to compensate.

When lock char is '1', increment lockCount if s char is '(', decrement lockCount if s char is ')'.

If flipCount + lockCount < 0, which means more locked ')' and there are not enough flexible chars on the left to compensate '('. return false.

After iteration, if lockCount > flipCount, which mean there are more locked '(' that can't be compensated. return false.
But this can't cover case like "))((" "0011". flipCount = 2 and lockCount = 2.

Thus we need to iterate from right to left, vice versa.

Time Complexity: O(n). n = s.length().

Spce: O(1).

AC Java:

 1 class Solution {
 2     public boolean canBeValid(String s, String locked) {
 3         if(s == null || s.length() == 0){
 4             return true;
 5         }
 6 
 7         if(s.length() % 2 == 1){
 8             return false;
 9         }
10 
11         int lockCount = 0;
12         int flipCount = 0;
13         for(int i = 0; i < s.length(); i++){
14             if(locked.charAt(i) == '0'){
15                 flipCount++;
16             }else{
17                 if(s.charAt(i) == '('){
18                     lockCount++;
19                 }else{
20                     lockCount--;
21                 }
22             }
23 
24             if(flipCount + lockCount < 0){
25                 return false;
26             }
27         }
28 
29         if(flipCount < lockCount){
30             return false;
31         }
32 
33         lockCount = 0;
34         flipCount = 0;
35         for(int i = s.length() - 1; i>=0; i--){
36             if(locked.charAt(i) == '0'){
37                 flipCount++;
38             }else{
39                 if(s.charAt(i) == ')'){
40                     lockCount++;
41                 }else{
42                     lockCount--;
43                 }
44             }
45 
46             if(flipCount + lockCount < 0){
47                 return false;
48             }
49         }
50 
51         if(flipCount < lockCount){
52             return false;
53         }
54 
55         return true;
56     }
57 }

类似Valid Parentheses.

posted @ 2024-02-25 03:01  Dylan_Java_NYC  阅读(8)  评论(0编辑  收藏  举报