leetcode - 2116. Check if a Parentheses String Can Be Valid

news/2024/11/28 1:03:24/

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 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.

Constraints:

n == s.length == locked.length
1 <= n <= 10^5
s[i] is either '(' or ')'.
locked[i] is either '0' or '1'.

Solution

Greedy, similar to 678. Valid Parenthesis String. When iterating from left to right, if there’s ), we would prefer to use ( first, if we don’t have enough (, start using unlocked indexes. After iteration, the remaining ( should be paired with locked indexes. And in the end, the remaining unlocked indexes should be even length so they can pair themselves.

Use 2 stacks to store the ( and the unlocked indexes.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

Code

class Solution:def canBeValid(self, s: str, locked: str) -> bool:open_p, unlocked_p = [], []for i in range(len(s)):if locked[i] == '0':unlocked_p.append(i)else:if s[i] == '(':open_p.append(i)else:if open_p:open_p.pop()elif unlocked_p:unlocked_p.pop()else:return Falsewhile open_p:cur_open_i = open_p.pop()if unlocked_p and unlocked_p[-1] > cur_open_i:unlocked_p.pop()else:return Falsereturn len(unlocked_p) & 1 == 0

http://www.ppmy.cn/news/1550477.html

相关文章

如何寻找适合的HTTP代理IP资源?

一、怎么找代理IP资源&#xff1f; 在选择代理IP资源的时候&#xff0c;很多小伙伴往往将可用率作为首要的参考指标。事实上&#xff0c;市面上的住宅IP或拨号VPS代理IP资源&#xff0c;其可用率普遍在95%以上&#xff0c;因此IP可用率并不是唯一的评判标准 其实更应该关注的…

网络安全的学习方向和路线是怎么样的?

最近有同学问我&#xff0c;网络安全的学习路线是怎么样的&#xff1f; 废话不多说&#xff0c;先上一张图镇楼&#xff0c;看看网络安全有哪些方向&#xff0c;它们之间有什么关系和区别&#xff0c;各自需要学习哪些东西。 在这个圈子技术门类中&#xff0c;工作岗位主要有以…

C#上机练习66-70

66.数组x中存有20个四位整数&#xff0c;请编制函数&#xff0c;求出正整数的个数tn。以及各位数字之和是偶数的数的个数tc,以及满足条件的这些数的算术平均ta.&#xff0c;将tn&#xff0c;tc&#xff0c;ta在控制台输出。 67.数组x中存有20个四位整数&#xff0c;请编制函数…

直接调用本地API(NTAPI)

文章目录 Windows操作系统中的功能调用流程直接调用NTAPI的重要性 Windows操作系统中的功能调用流程 在Windows操作系统中&#xff0c;应用程序与操作系统内核之间的交互是通过一系列精心设计的函数调用流程来实现的。让我们以一个常见的操作——创建文件为例&#xff0c;来详…

Windows系统安装docker教程

使用WSL 2安装docker 一.安装wsl2 1.在任务管理器中&#xff0c;检查系统虚拟化是否开启 ​ 2.以管理员身份运行cmd. 3.输入&#xff1a;wsl --install 4.启用wsl功能 dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart …

基于 Spring AOP (面向切面编程) 的切面类--自定义方法注解的方式打印日志

java每日一学 ---- 面向切面编程 怎么通过方法注解的方式打印日志呢&#xff1f;自定义注解 1、首先定义一个ExecuteLog 注解类 Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface ExecuteLog {/*** 执行方法的描述&#xff0c;默认方法名**…

计算机网络 第4章 网络层

计算机网络 &#xff08;第八版&#xff09;谢希仁 第 4 章 网络层4.2.2 IP地址**无分类编址CIDR**IP地址的特点 4.2.3 IP地址与MAC地址4.2.4 ARP 地址解析协议4.2.5 IP数据报的格式题目2&#xff1a;IP数据报分片与重组题目&#xff1a;计算IP数据报的首部校验和(不正确未改) …

SQL for XML

关系数据模型与SQL SQL for XML 模式名功能RAW返回的行作为元素&#xff0c;列值作为元素的属性AUTO返回表名对应节点名称的元素&#xff0c;每列的属性作为元素的属性输出输出&#xff0c;可形成简单嵌套结构EXPLICIT通过SELECT语法定义输出XML结构PATH列名或列别名作为XPAT…