当前位置: 首页 > news >正文

leetcode - 678. Valid Parenthesis String

Description

Given a string s containing only three types of characters: ‘(’, ‘)’ and ‘*’, return true if s is valid.

The following rules define a valid string:

Any left parenthesis '(' must have a corresponding right parenthesis ')'.
Any right parenthesis ')' must have a corresponding left parenthesis '('.
Left parenthesis '(' must go before the corresponding right parenthesis ')'.
'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "(*)"
Output: true

Example 3:

Input: s = "(*))"
Output: true

Constraints:

1 <= s.length <= 100
s[i] is '(', ')' or '*'.

Solution

Stack

Use 2 stacks, one for opening parenthesis, one for stars. When the current character is a closing parenthesis, pop from the opening parenthesis first, then the star. After iterating the string, pop from the opening parenthesis and star, for every opening parenthesis, if there is a star at the right, then it’s valid.

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

Two pointers

Solved after help.

Use cmin to denote the count of ( we must pair, and cmax to denote the count of ( at most we need to pair. So when the current character is (, we increase both of them by 1, when it’s ) we decrease by 1, when it’s *, we decrease cmin by 1 (the star will work as )), and increase cmax by 1 (the star will work as ().

During this process, the cmax can’t be negative, and in the end, the cmin needs to be 0.

Here’s a graph from this solution:
在这里插入图片描述
Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

Stack

class Solution:def checkValidString(self, s: str) -> bool:star_stack = []opening_stack = []for i, each_c in enumerate(s):if each_c == '(':opening_stack.append(i)elif each_c == ')':if opening_stack:opening_stack.pop()elif star_stack:star_stack.pop()else:return Falseelse:star_stack.append(i)while opening_stack:if not star_stack:return Falseopening_i, star_i = opening_stack.pop(), star_stack.pop()if star_i < opening_i:return Falsereturn True

Two pointers

class Solution:def checkValidString(self, s: str) -> bool:cmin, cmax = 0, 0for each_c in s:if each_c == '(':cmin += 1cmax += 1elif each_c == ')':cmin -= 1cmax -= 1else:cmin -= 1cmax += 1if cmax < 0:return Falsecmin = max(0, cmin)return cmin == 0
http://www.lryc.cn/news/333871.html

相关文章:

  • 索尼相机照片清理软件
  • 比赛记录:Codeforces Global Round 25 A~E (猜猜题场)
  • Windows系统安装OpenSSH结合VS Code远程ssh连接Ubuntu【内网穿透】
  • Svg Flow Editor 原生svg流程图编辑器(五)
  • 数字晶体管选型参数,结构原理,工艺与注意问题总结
  • lua学习笔记9(字典的学习)
  • 第六篇: 3.5 性能效果 (Performance)- IAB/MRC及《增强现实广告效果测量指南1.0》
  • mysql学习笔记NO.2
  • C++11:lambda表达式 包装器
  • Node.js HTTP/2 CONTINUATION 拒绝服务漏洞(CVE-2024-27983)
  • YOLOV8 + 双目测距
  • 前端:SVG绘制流程图
  • 【Linux系列】如何确定当前运行的是 RHEL 9 还是 RHEL 8?
  • vscode开发java的插件和配置
  • Mysql启动报错:本地计算机上的mysql服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
  • WPF程序添加托盘图标
  • 工业4g路由器联网后迅速掉线是什么原因?
  • 腾讯云4核8G服务器12M带宽646元1年零3个月,4C8G使用场景说明
  • java - 读取配置文件
  • Ubuntu22.04平台编译完美解决问题“error: GLSL 4.5 is not supported.”【GLSL(OpenGL着色器语言)】
  • 数据结构之搜索二叉树与关联性容器初接触
  • C语言整数和小数的存储
  • Games101Homework【6】Acceleration structure(Including framework analysis)
  • 应用运维文档1
  • 手机如何在线制作gif?轻松一键在线操作
  • ChatGPT 在做什么,为什么有效?
  • Linux实验2 初步使用shell
  • 甘特图/横道图制作技巧 - 任务组
  • Web题记
  • 学习java第三十六天