Skip to content

678. Valid Parenthesis String #21741

@AdityaKonda6

Description

@AdityaKonda6

LeetCode Username

Aditya Konda

Problem Number, Title, and Link

678. Valid Parenthesis String

Bug Category

Problem description, Problem examples, Problem constraints, Problem hints, Incorrect or missing "Related Topics", Incorrect test case (Output of test case is incorrect as per the problem statement), Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases), Editorial

Bug 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

Language Used for Code

Java

Code used for Submit/Run operation

class Solution {
  public boolean checkValidString(final String s) {
    int low = 0;
    int high = 0; 
    for (final char c : s.toCharArray()) {
      switch (c) {
        case '(':
          ++low;
          ++high;
          break;
        case ')':
          low = Math.max(0, --low);
          --high;
          break;
        case '*':
          low = Math.max(0, --low);
          ++high;
          break;
      }
      if (high < 0)
        return false;
    }
  return low == 0;
  }
}

Expected behavior

Day48

Screenshots

Day48

Additional context

Day48.docx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions