Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check if a Parentheses String Can Be Valid

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2116" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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 n. locked 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. Example 4: Input: s = "(((())(((())", locked = "111111010111" Output: true Explanation: locked permits us to change s[6] and s[8]. We change s[6] and s[8] to ')' to make s valid. Constraints: n == s.length == locked.length 1 <= n <= 105 s[i] is either '(' or ')'. locked[i] is either '0' or '1'.

Explanation

Here's the solution:

  • Greedy Approach: Iterate through the string, maintaining a balance. Increment the balance for '(' and decrement for ')'. If the balance becomes negative, try to flip unlocked '(' to ')' to restore the balance.
  • Sufficient Flexibility Check: After the initial pass, check if we have enough unlocked characters to correct any remaining imbalance.
  • Reverse Pass: Perform a reverse iteration to ensure we can correct for a potential imbalance from the other direction.

  • Runtime Complexity: O(n), Storage Complexity: O(1)

Code

    def an_make_valid(s: str, loked: str) -> bool:    n = len(s)
    if n % 2 != 0:
        return False

    def check_validity(s, locked):
        balance = 0
        unlocked_count = 0
        for i in range(n):
            if s[i] == '(':
                balance += 1
            else:
                balance -= 1

            if locked[i] == '0':
                unlocked_count += 1

            if balance < 0:
                if unlocked_count >= abs(balance):
                    unlocked_count -= abs(balance)
                    balance = 0
                else:
                    return False

        return balance <= unlocked_count

    if not check_validity(s, locked):
        return False


    def check_validity_reverse(s, locked):
        balance = 0
        unlocked_count = 0
        for i in range(n - 1, -1, -1):
            if s[i] == ')':
                balance += 1
            else:
                balance -= 1

            if locked[i] == '0':
                unlocked_count += 1

            if balance < 0:
                if unlocked_count >= abs(balance):
                    unlocked_count -= abs(balance)
                    balance = 0
                else:
                    return False

        return balance <= unlocked_count

    if not check_validity_reverse(s, locked):
        return False

    return True

More from this blog

C

Chatmagic blog

2894 posts