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:

  1. Any left parenthesis '(' must have a corresponding right parenthesis ')'.
  2. Any right parenthesis ')' must have a corresponding left parenthesis '('.
  3. Left parenthesis '(' must go before the corresponding right parenthesis ')'.
  4. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
  5. An empty string is also valid.

Example 1:

  1. Input: "()"
  2. Output: True

Example 2:

  1. Input: "(*)"
  2. Output: True

Example 3:

  1. Input: "(*))"
  2. Output: True

题目

验证有效括号字符串

思路

recursion

类似Leetcode 22 Generate Parenthesis 思路

代码

  1. class Solution {
  2. public boolean checkValidString(String s) {
  3. return check(s, 0, 0);
  4. }
  5.  
  6. private boolean check(String s, int start, int count) {
  7. if (count < 0) return false;
  8.  
  9. for (int i = start; i < s.length(); i++) {
  10. char c = s.charAt(i);
  11. if (c == '(') {
  12. count++;
  13. }
  14. else if (c == ')') {
  15. if (count <= 0) return false;
  16. count--;
  17. }
  18. else if (c == '*') {
  19. //1. * for '(' --> (*))
  20. //2. * for ')' --> ((*)
  21. //3. * for empty string --> (*)
  22. return check(s, i + 1, count + 1) || check(s, i + 1, count - 1) || check(s, i + 1, count);
  23. }
  24. }
  25.  
  26. return count == 0;
  27. }
  28.  
  29. }

[leetcode]678. Valid Parenthesis String验证有效括号字符串的更多相关文章

  1. [LeetCode] 678. Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  2. leetcode 678. Valid Parenthesis String

    678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...

  3. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  4. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. 678. Valid Parenthesis String

    https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...

  6. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  7. 【leetcode】Valid Parenthesis String

    题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...

  8. LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  9. [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

随机推荐

  1. unity admob

    插件地址:https://github.com/unity-plugins/Unity-Admob 2017.04.11测试使用发现GoogleMobileAds.framework有问题,导致出现U ...

  2. Spring STS Call Hierarchy 查找不到被调用的信息

    今天使用Spring的STS的时候,发现Call  Hierarchy无法使用,很奇怪,发现问题出现在同一个工作区间里,如果工作区间不在此工作区间,发现还是可以找到被调用的信息的.当时在网上找也没找到 ...

  3. mysql 忘记密码解决方案

    Mysql 忘记root密码的完美解决方法 转载  2016-12-23   作者:MR.QiGao    我要评论 通常在使用Mysql数据库时,如果长时间没有登陆,或者由于工作交接完成度不高,会导 ...

  4. 13.BeanUtils组件-基础.md

    目录 用途 基本属性的设置 Map数据的拷贝 对象的拷贝 转换器 用途 可以用来对JavaBean的各种增强操作 基本属性的设置 package per.liyue.code.beanutildemo ...

  5. Uni2D 入门 -- Atlas转载 http://blog.csdn.net/kakashi8841/article/details/17588095

    转载csdnTexture Atlas 我为什么应该使用Texture Atlas? 使用Atlas是一个普遍的好做法,而且它有很多好处.当有某些需要在屏幕渲染的时候,它背后带来的是draw call ...

  6. 吴裕雄 python深度学习与实践(2)

    #coding = utf8 import threading,time,random count = 0 class MyThread (threading.Thread): def __init_ ...

  7. python 自然语言处理库https://www.nltk.org/nltk_data/

    https://www.nltk.org/nltk_data/ https://github.com/hankcs/HanLP

  8. 11.14java课堂测试

    源代码: import java.*; import java.util.*; import java.io.File; import java.io.FileInputStream; import ...

  9. 渲染Keynote

    [渲染Keynote] 1.渲染图元(rendering primitives),可以是点.线.三角. 2.显卡对于显存的访问速度更快,而且大多数显卡对于RAM没有直接的访问权利 . 3.裁剪(Cli ...

  10. msf客户端渗透(四):关闭UAC,hash、防火墙、服务、磁盘、DEP、防病毒、远程桌面、桌面截图、获取tooken

    关闭UAC 如果你获得一个system权限的session 进入到这个session 进入到shell 依次输入以下命令 cmd.exe /k %windir%\System32\reg.exe AD ...