diff options
author | Jack Sangdahl <[email protected]> | 2025-01-11 12:58:39 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-01-13 13:02:43 -0700 |
commit | 32b3c8cc7a72294298aabcedc2033b06f2a7ca5e (patch) | |
tree | ff4e5338da75dbe7e024a2d2fd3f0c8b1262e796 | |
parent | 86fae540ca9b40b8294541593b32600549c5a653 (diff) | |
download | leetcode-32b3c8cc7a72294298aabcedc2033b06f2a7ca5e.tar.gz leetcode-32b3c8cc7a72294298aabcedc2033b06f2a7ca5e.zip |
2116. check if a parentheses string can be valid
-rw-r--r-- | check_parentheses_valid.c | 50 | ||||
-rw-r--r-- | check_parentheses_valid.py | 49 |
2 files changed, 99 insertions, 0 deletions
diff --git a/check_parentheses_valid.c b/check_parentheses_valid.c new file mode 100644 index 0000000..06efb94 --- /dev/null +++ b/check_parentheses_valid.c @@ -0,0 +1,50 @@ +// 2116. Check if a Parentheses String Can Be Valid +#include "leetcode.h" +#include <math.h> +#include <string.h> + +/* + * 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' where 'A' and 'B' are valid parentheses, 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 'n'. 'locked' is a binary + * string consistion only of '0's and '1's. for each indiex 'i' of 'locked'. + */ + +bool canBeValid(char *s, char *locked) { + int n = strlen(s), total = 0, open = 0, closed = 0; + if (n % 2 == 1) + return false; + for (int i = n - 1; i >= 0; i--) { + if (locked[i] == '0') + total++; + else if (s[i] == '(') + open++; + else if (s[i] == ')') + closed++; + if (total - open + closed < 0) + return false; + } + total = open = closed = 0; + for (int i = 0; i < n; i++) { + if (locked[i] == '0') + total++; + else if (s[i] == '(') + open++; + else if (s[i] == ')') + closed++; + if (total + open - closed < 0) + return false; + } + return true; +} + +int main() { + char *s1 = "))()))", *l1 = "010100"; + char *s2 = "()()", *l2 = "0000"; + char *s3 = ")", *l3 = "0"; + printf("%d\n", canBeValid(s1, l1)); // expect: 1 + printf("%d\n", canBeValid(s2, l2)); // expect: 1 + printf("%d\n", canBeValid(s3, l3)); // expect: 0 +} diff --git a/check_parentheses_valid.py b/check_parentheses_valid.py new file mode 100644 index 0000000..a6f5c28 --- /dev/null +++ b/check_parentheses_valid.py @@ -0,0 +1,49 @@ +# 2116. Check if a Parentheses String Can Be Valid + +""" +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' where 'A' and 'B' are valid parentheses, 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 'n'. 'locked' is a binary +string consistion only of '0's and '1's. for each indiex 'i' of 'locked'. +""" + + +class Solution(object): + def canBeValid(self, s, locked): + """ + :type s: str + :type locked: str + :rtype: bool + """ + if len(s) % 2 == 1: + return False + total = open = closed = 0 + for i in range(len(s) - 1, -1, -1): + if locked[i] == "0": + total += 1 + elif s[i] == "(": + open += 1 + elif s[i] == ")": + closed += 1 + if total - open + closed < 0: + return False + total = open = closed = 0 + for i in range(len(s)): + if locked[i] == "0": + total += 1 + elif s[i] == "(": + open += 1 + elif s[i] == ")": + closed += 1 + if total + open - closed < 0: + return False + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.canBeValid(s="))()))", locked="010100")) + print(obj.canBeValid(s="()()", locked="0000")) + print(obj.canBeValid(s=")", locked="0")) |