Similar Problems

Similar Problems not available

Check If There Is A Valid Parentheses String Path - Leetcode Solution

Companies:

LeetCode:  Check If There Is A Valid Parentheses String Path Leetcode Solution

Difficulty: Hard

Topics: matrix dynamic-programming array  

Problem Statement:

Given a string path of parentheses, determine if it is a valid parentheses string.

A valid parentheses string is either:

  • An empty string ""
  • A string consisting of parentheses '(' and ')' which belong to the same level of nesting and have the same position relative to the center of the string, i.e. balanced parentheses is such a string: "(())", but unbalanced parentheses is such a one: "())(()". Note that any parentheses between between two balanced parentheses are also balanced: "()()"; "(())()". The order of the parentheses does not matter.
  • A string consisting of lowercase English letters ['a'-'z'] only

Return true if path is a valid parentheses string, otherwise return false.

Constraints:

  • 1 <= path.length <= 10^4
  • path[i] is either '(' or ')'.
  • path is a valid parentheses string or path is a valid sequence of lowercase letters.

Solution:

One way to solve this problem is by using stack data structure. We can push an opening parentheses onto the stack, and whenever we encounter a closing parentheses, we pop the topmost element from the stack. If the popped element is not an opening parentheses of the same type, or if the stack is empty, then the parentheses in path is invalid. In the end, if the stack is empty, we have a valid parentheses string.

However, in this problem, we also need to consider letters. If a letter is encountered, it can be ignored, as it has no effect on the validity of the parentheses. Therefore, we need to modify our stack solution to ignore letters.

We can keep two counters, one for opening parentheses and the other for closing parentheses. Whenever we encounter a letter, we ignore it and continue. If we encounter an opening parentheses, we increment the opening counter, and if we find a closing parentheses, we increment the closing counter. If at any point, the closing counter is greater than the opening counter, it means that there are more closing parentheses than opening parentheses, and hence, the string is not valid. Also, if the opening counter is greater than the closing counter at the end, the string is also not valid.

Python Code:

def isValidPath(path: str) -> bool: opening_count = 0 closing_count = 0

for c in path:
    if c == '(':
        opening_count += 1
    elif c == ')':
        closing_count += 1
    # Ignore letters
    else:
        continue
    
    # Closing count should not exceed opening count
    if closing_count > opening_count:
        return False

# If opening count is greater than closing count, not valid
if opening_count > closing_count:
    return False

# Valid parentheses string
return True

Note: This solution has a time complexity of O(n), where n is the length of the path string. This is because we are iterating through each character in the string once.

Check If There Is A Valid Parentheses String Path Solution Code

1