Skip to content

Latest commit

 

History

History
 
 

678. Valid Parenthesis String

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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

 

Constraints:

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

Companies:
Apple, Amazon, Microsoft, Facebook

Related Topics:
String, Dynamic Programming, Stack, Greedy

Similar Questions:

TLE Version

This solution is not performant if s contains lots of "*".

// OJ: https://leetcode.com/problems/valid-parenthesis-string/
// Author: github.com/lzl124631x
// Time: O(3^S) where S is the length of string s. In the worst case every character
//       is "*", so every step has 3 choices.
// Space: O(S)
class Solution {
private:
    bool dfs(string &s, int start, int leftParenCnt) {
        if (start == s.size()) return !leftParenCnt;
        if (s[start] == '(') {
            return dfs(s, start + 1, leftParenCnt + 1);
        } else if (s[start] == ')') {
            if (--leftParenCnt < 0) return false;
            return dfs(s, start + 1, leftParenCnt);
        } else {
            if (dfs(s, start + 1, leftParenCnt + 1)) return true;
            if (leftParenCnt >= 1 && dfs(s, start + 1, leftParenCnt - 1)) return true;
            return dfs(s, start + 1, leftParenCnt);
        }
    }
public:
    bool checkValidString(string s) {
        return dfs(s, 0, 0);
    }
};

Solution 1.

Let diff be count of left parenthesis minus count of right parenthesis.

When we meet:

  • (, we increment diff.
  • ), we decrement diff.
  • *, we have three choices which makes the diff become a range -- [diff - 1, diff + 1].

So we use maxDiff/minDiff to record the maximum/minimum diff we can get.

When we meet:

  • (, ++maxDiff and ++minDiff.
  • ), --maxDiff and --minDiff.
  • *, ++maxDiff and --minDiff.

If maxDiff become negative, it means it's already invalid, we should return false.

Whenever minDiff falls below 0, we should force it to be 0 because we never accept negative diff during the process.

After scanning through string s, as long as minDiff is 0, this string can be a valid one.

Whenever minDiff falls below 0, we should force it to be 0 because we never accept negative diff during the process.

minDiff means the diff we got if we always try to replace * with ). If minDiff become -1, it means that this replacement results in more ) than (, so it should be avoided. To avoid it, we simply reset minDiff from -1 to 0 which implies we only replace * with ( or empty string.

Example: (**)

  • Seeing (, the range becomes [1, 1].
  • Seeing *, the range becomes [0, 2]. 0 correponds to (), 1 to (_, 2 to ((.
  • Seeing *, the range becomes [-1,3]. But -1 is invalid because it means ()) and should be avoided. So we correct the range to [0, 3].
  • Seeing ), the range becomes [-1, 2]. Again, we correct the range to [0, 2] (because -1 means ()_) or (_)))

The final [0, 2] range means that we can either get a perfect string, or has 1 or 2 more ( available (which are created by *).

// OJ: https://leetcode.com/problems/valid-parenthesis-string/
// Author: github.com/lzl124631x
// Time: O(S)
// Space: O(1)
class Solution {
public:
    bool checkValidString(string s) {
        int maxDiff = 0, minDiff = 0;
        for (char c : s) {
            maxDiff += (c == '(' || c == '*') ? 1 : -1;
            minDiff += (c == ')' || c == '*') ? -1 : 1;
            if (maxDiff < 0) return false;
            if (minDiff < 0) minDiff = 0;
        }
        return minDiff == 0;
    }
};

NOTE:

My post: https://leetcode.com/problems/valid-parenthesis-string/discuss/302732/C%2B%2B-O(S)-Time-O(1)-Space-One-Pass-with-Explanation