Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Created January 12, 2025 18:35
Show Gist options
  • Save lbvf50mobile/d2b23d93bb6ae952dd43e03c2a57f505 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/d2b23d93bb6ae952dd43e03c2a57f505 to your computer and use it in GitHub Desktop.
Leetcode: 2116. Check if a Parentheses String Can Be Valid
// Leetcode: 2116. Check if a Parentheses String Can Be Valid
// https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/description
// = = = = = = = = = = = = = =
// Accepted.
// Thanks God, Jesus Christ!
// = = = = = = = = = = = = = =
// Runtime: 5 ms Beats 75.00%
// Memory: 8.81 MB Beats 75.00%
// 2025.01.12 Daily Challenge.
package main
import (
// "fmt"
)
func canBeValid(s string, locked string) bool {
// Hint: Possible to open first, not close first.
n := len(s)
if 1 == n%2 {
return false
}
flex, open := 0, 0
for i := 0; i < n; i += 1 {
if '0' == locked[i] {
flex += 1
continue
}
if '(' == s[i] {
open += 1
continue
}
if ')' == s[i] && open > 0 {
open -= 1
continue
}
if ')' == s[i] && flex > 0 {
flex -= 1
continue
}
return false
}
if open > flex {
return false
}
open, flex = 0, 0
for i := n - 1; i >= 0; i -= 1 {
if '0' == locked[i] {
flex += 1
continue
}
if ')' == s[i] {
open += 1
continue
}
if '(' == s[i] && open > 0 {
open -= 1
continue
}
if '(' == s[i] && flex > 0 {
flex -= 1
continue
}
return false
}
if open > flex {
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment