32 · Greedy Algorithms

#678 Valid Parenthesis String

Mediumsolve on LeetCode ↗

frequently reported atByteDance / TikTokAmazonMetaGoogle

the problem

In plain words

A string s has only three kinds of characters: '(', ')' and ''. Each '' can stand for '(' or ')' or nothing at all. Return true if some choice for the stars makes s balanced: every '(' is closed by a later ')', and no ')' comes without an open '(' before it.

in s = "()"

out true

in s = "(*)"

out true

The star is nothing.

in s = "(*))"

out true

The star is '(': "(())".

in s = "("

out false

  • 1 ≤ s.length ≤ 100
  • s[i] is '(', ')' or '*'

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #20
      Valid Parentheses
      Easyin Stacks
    2. #22
      Generate Parentheses
      Mediumin Backtracking