当前位置:网站首页>LeetCode quiz series -- 678. Valid bracket strings
LeetCode quiz series -- 678. Valid bracket strings
2022-08-07 15:33:00 【Wood water in the river of state】
Given a string containing only three characters: ( , ) and *, write a function to check if the string is a valid string.Valid strings have the following rules:
Any opening parenthesis (must have a corresponding closing parenthesis ).
Any closing bracket) must have a corresponding opening bracket ( .
A opening bracket (must precede the corresponding closing bracket).
* can be treated as a single closing bracket) , or a single opening bracket ( , orAn empty string.
An empty string is also considered a valid string.
Example 1:
Input: "()"
Output: True
Example 2:
Input: "(*)"
Output: True
Example 3:
Input: "(*))"
Output: True
Source: LeetCode
Link: https://leetcode.cn/problems/valid-parenthesis-string
Thinking:
1. Define two stacks, denoted as stack_a and stack_b respectively.2. Traverse the string:2.1) When the character is ( , push the current position index of the character into stack_a2.2) When the character is *, push the current position index of the character into stack_b2.3) When the character is ),If stack_a is not empty, pop the top element of stack_a; (try to match the left parenthesis first)If stack_a is empty and stack_b is not empty, the top element of stack_a will be popped; (when the left parentheses are no longer matched, continue to match *, because * no matter how many are left)If stack_a is empty and stack_b is empty, it means that there is no '(' or '*' matching ')' , indicating that the string does not meet the requirements, return false3. If stack_a is not empty:Then pop up the top of stack_a, and compare it with the top element of stack_b. If the value of the top of stack_b is larger than that of stack_a, it means that the index with the * character is behind the ( , which can form valid parentheses;Otherwise, if the string does not meet the requirements, return falsejava:
class Solution {public boolean checkValidString(String s) {boolean result = true;Stack stackA = new Stack<>();Stack stackB = new Stack<>();for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == '(') {stackA.push(i);} else if (s.charAt(i) == '*') {stackB.push(i);} else {if (!stackA.empty()) {stackA.pop();} else if (!stackB.empty()) {stackB.pop();} else {// The current character is a right parenthesis, and there is no ( or * corresponding to it on the left side, indicating that a valid parenthesis cannot be formedreturn false;}}}// Obviously if the number of ( is less than * , then it must be invalid parenthesesif(stackA.size()>stackB.size()) {return false;}// if ( is left, then match *while (!stackA.empty() && !stackB.empty()) {if(stackA.peek() 边栏推荐
- mysql5.7安装和配置教程(图文超详细版)
- leetcode 485:最大连续 1 的个数
- Hash table | 1. The sum of two numbers, 454. The addition of four numbers | The most suitable `dictionary key-value` | leecode brush the notes
- RTT学习笔记7-中断管理
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- The two day summary ([18], [19])
- 03 "commonly used types (below)"
- ETCD快速入门-01 ETCD概述
- win10 uwp 关联文件
- 做一个物联网云平台到底要多少钱?
猜你喜欢

The ADC external RC circuit resistance and capacitance selection calculation method

亚马逊云科技 Build On 参与心得

LeetCode HOT HOT 100 (9. Telephone number letter combinations)

冒泡排序的理解

LeetCode 热题 HOT 100(10.删除链表的倒数第 N 个结点)

Redis的下载与安装Windows和Linux版

Summary of the open surface

WeChat automatic card issuance robot description

JWT的创建

【IROS 2019】RangeNet++: 快速准确的LiDAR语义分割
随机推荐
Pytorch介绍以及基本使用
win10 uwp 在 Grid 接收键盘消息
MySQL的UPDATE及SELECT...FOR UPDATE语句关于锁的一些简单验证
LeetCode 热题 HOT 100(8.三数之和)
Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.1 The Portzebie Weights and Measures System
5步详解如何运用设计思维
Browser working principle and practical study notes (1) Browser from a macro perspective
03 "commonly used types (below)"
Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.4 The Pain of Prototypes
Chapter 8 C programming expert why programmers cannot distinguish where Halloween and Christmas 8.5 prototype fail
LeetCode每日一题(911. Online Election)
Short note_Altium Designer layout and routing reuse of the same module
pip使用豆瓣镜像源
【数据库系统原理】第四章 高级数据库模型:弱实体集、E/R 联系到关系的转化、子类结构到关系的转化
网页模板 pug 基本语法
LeetCode 热题 HOT 100(4.寻找两个正序数组的中位数)
[Advanced Mathematics] Advanced Number Arrangement: Common Equivalent Infinitesimals, Derivatives and Differentials, Differential Equations
labelme安装
mysql5.7安装和配置教程(图文超详细版)
【通信原理】第三章 -- 随机过程[补充]