首页 > 其他分享 >LWC 50:678. Valid Parenthesis String

LWC 50:678. Valid Parenthesis String

时间:2023-07-10 16:32:28浏览次数:35  
标签:right 678 String 50 parenthesis cs return string left


LWC 50:678. Valid Parenthesis String

传送门:678. Valid Parenthesis String

Problem:

Given a string containing only three types of characters: ‘(‘, ‘)’ and ‘*’, write a function to check whether this string is valid. We define the validity of a string by these rules:

  • 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.
  • An empty string is also valid.

Example 1:

Input: “()”
Output: True

Example 2:

Input: “(*)”
Output: True

Example 3:

Input: “(*))”
Output: True

Note:

  • The string size will be in the range [1, 100].
    Discuss

思路:
采用暴力搜索,真正需要遍历的状态是”*”,每次遇到星,都有三种状态:1. 不做任何操作,2. 左括号+1, 3. 右括号+1,合法状态为左括号始终大于等于右括号,且最终输出left == right。

代码如下:

public boolean checkValidString(String s) {
        return robot(s.toCharArray(), 0, 0, 0);
    }

    boolean robot(char[] cs, int i, int left, int right) {
        if (i >= cs.length) {
            return left == right;
        }
        for (int j = i; j < cs.length; ++j) {
            if (cs[j] == '(')
                left++;
            else if (cs[j] == ')') {
                right++;
                if (right > left) return false;
            } 
            else {
                return robot(cs, j + 1, left, right) || robot(cs, j + 1, left + 1, right)
                        || robot(cs, j + 1, left, right + 1);
            }
        }
        return left == right;
    }


标签:right,678,String,50,parenthesis,cs,return,string,left
From: https://blog.51cto.com/u_16184402/6678327

相关文章

  • LWC 50:677. Map Sum Pairs
    LWC50:677.MapSumPairs传送门:677.MapSumPairsProblem:ImplementaMapSumclasswithinsert,andsummethods.Forthemethodinsert,you’llbegivenapairof(string,integer).Thestringrepresentsthekeyandtheintegerrepresentsthevalue.Ifthekey......
  • String s=new String(“hello”)的执行过程
    一.介绍String是Java.long包下的String类,是一个特殊的引用类型,用于表示字符串。它提供了许多方法来操作和处理字符串,比如连接、截取、查找、替换等。String类内部使用字符数组(char[])来存储字符串的内容,value字段被final修饰,String对象一旦创建后,其值就不可改变。Strin......
  • 单部六层(1200系列、1500系列都有可仿真 ),六部十层1200系列。
    单部六层(1200系列、1500系列都有可仿真),六部十层1200系列。有较大参考性。YID:6315645040008490......
  • 西门子PID调节仿真程序,1200plc和1500plc通用,只需一个PLC实物,就能轻松实现PID工艺对象
    西门子PID调节仿真程序,1200plc和1500plc通用,只需一个PLC实物,就能轻松实现PID工艺对象的仿真,是学习PID的参数的好工具。针对这套程序,录制了一段视频解说,手把手教你如何使用博途PID调节工具和触摸屏PID画面的操作,非常值得拥有哦ID:7115632550149443......
  • String 源码阅读
    String源码阅读1.属性/**Thevalueisusedforcharacterstorage.*/privatefinalcharvalue[];/**Cachethehashcodeforthestring*/privateinthash;//Defaultto0/**useserialVersionUIDfromJDK1.0.2forinteroperability*/privatestaticfi......
  • CF559B - Equivalent Strings
    首先我们考虑第一种做法,我们搜索\(dp_{x,y,l,r}\)判断\(s[x,y]\)和\(t[l,r]\)是否等价,同时记忆化搜索。但是这样是很明显不行的。如果长度是\(2\)的整次幂,我们仅分析最底层长度为\(1\)的区间,我们发现,任何的\([x,x][y,y](x\len/2)\),都会被搜到一遍。这个可以递归处理,......
  • String、StringBuffer、StringBuilder 的区别?
    一.介绍String、StringBuffer、StringBuilder:  前言: String、StringBuffer、StringBuilder均在java.lang包下;String: 在Java中,String是一个特殊的引用类型,用于表示文本字符串。它提供了许多方法来操作和处理字符串,比如连接、截取、查找、替换等。String类......
  • 【cs50】lab7 & problem set7
    (1)lab7songssqlite3songs.db1)listthenamesofallsongsinthedatabaseSELECTnameFROMsongs;2)listnamesofallsongsinincreasingorderoftempoSELECTnameFROMsongsORDERBYtempo;3) listthenamesofthetop5longests......
  • String内存模型和Java常用方法
    一、String内存模型1、直接赋值创建string对象内存原理:StringTable(串池):字符串常量池,用来存储字符串,只能是在直接赋值中使用才会存在串池当中(JDK7前串池是在方法区里面,StringTable(串池)在JDK7版本开始从方法区中挪到了堆内存,但是运行机制没有发生变化)eg:首先mian方法进栈,创建变......
  • 最完美WIN10_Pro_22H2.19045.3155软件选装纯净版VIP50.7
    【系统简介】=============================================================1.本次更新母盘来自UUP_WIN10_PRO_22H2.19045.3155。进一步精简优化调整。2.只为呈现最好的作品,手工精简优化部分较多。3.OS版本号为19045.3155。个别要求高的就下MSDN吧,里面啥功能都有。4.集成《DrvCeo......