Skip to content

Latest commit

 

History

History
114 lines (100 loc) · 6.04 KB

CP_Problem_2.md

File metadata and controls

114 lines (100 loc) · 6.04 KB

Write the JAVA code for the below problem: 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 '*'. Use the following template: class Solution { public boolean checkValidString(String s) { } }

Here is the JAVA code to solve the problem:

kotlin

class Solution {
    public boolean checkValidString(String s) {
        // Count of open brackets
        int open = 0;
        // Count of asterisks
        int asterisks = 0;
        // Loop through each character in the string
        for (char c : s.toCharArray()) {
            if (c == '(') {
                // Increment the count of open brackets
                open++;
            } else if (c == ')') {
                // If there are no open brackets, then check if there are any asterisks and decrement their count
                if (open == 0) {
                    if (asterisks == 0) {
                        // If there are no asterisks, then this is an invalid string
                        return false;
                    } else {
                        // Decrement the count of asterisks
                        asterisks--;
                    }
                } else {
                    // Decrement the count of open brackets
                    open--;
                }
            } else if (c == '*') {
                // Increment the count of asterisks
                asterisks++;
            }
        }
        // Now we need to check for remaining open brackets
        if (open > 0) {
            // If there are open brackets and asterisks, then we can use asterisks as right parentheses to balance the string
            if (asterisks >= open) {
                return true;
            } else {
                // Otherwise, this is an invalid string
                return false;
            }
        }
        // Otherwise, the string is valid
        return true;
    }
}

The above code maintains two variables open and asterisks to keep track of the number of open brackets and asterisks in the string respectively. It loops through each character in the string and for each character, it increments/decrements the count of open brackets and asterisks accordingly.

If a closing bracket is encountered and there are no open brackets, then it checks if there are any asterisks available. If there are no asterisks available, then it returns false as the string is invalid. Otherwise, it decrements the count of asterisks.

If an asterisk is encountered, it increments the count of asterisks.

After the loop, it checks if there are any remaining open brackets. If there are open brackets and asterisks available, then it returns true as the asterisks can be used as right parentheses to balance the string. If there are open brackets and no asterisks available, then it returns false as the string is invalid. If there are no remaining open brackets, then it returns true as the string is valid.

Modify the abpve code such that it also passes the below test case: Input s = "(((((*(()((((*((**(((()()*)()()()*((((**)())*)*)))))))(())(()))())((*()()(((()((()*(())*(()**)()(())" Output true Expected false

The code can be modified to pass the test case by using a different approach where we use two stacks - one to keep track of the indices of open brackets and the other to keep track of the indices of asterisks. We iterate through the string and do the following:

  • Whenever an open bracket is encountered, we push its index onto the open brackets stack.
  • Whenever an asterisk is encountered, we push its index onto the asterisks stack.
  • Whenever a closing bracket is encountered, we do the following:
    • If there are any open brackets on the stack, we pop the topmost one and continue.
    • If there are no open brackets on the stack but there are asterisks, we pop the topmost asterisk and continue.
    • If there are neither open brackets nor asterisks, the string is invalid and we return false.
  • After iterating through the string, we check if there are any remaining open brackets on the stack. For each remaining open bracket, we pop the topmost asterisk from the asterisks stack if it is not empty. If the asterisks stack is empty or there are more open brackets than asterisks, the string is invalid and we return false.

Here's the modified code:

vbnet

class Solution {
    public boolean checkValidString(String s) {
        Stack<Integer> openStack = new Stack<>();
        Stack<Integer> asteriskStack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                openStack.push(i);
            } else if (c == '*') {
                asteriskStack.push(i);
            } else {
                if (!openStack.isEmpty()) {
                    openStack.pop();
                } else if (!asteriskStack.isEmpty()) {
                    asteriskStack.pop();
                } else {
                    return false;
                }
            }
        }
        while (!openStack.isEmpty()) {
            if (asteriskStack.isEmpty()) {
                return false;
            } else if (openStack.peek() < asteriskStack.peek()) {
                openStack.pop();
                asteriskStack.pop();
            } else {
                return false;
            }
        }
        return true;
    }
}

With this modification, the function should correctly return false for the input string (((((*(()((((*((**(((()()*)()()()*((((**)())*)*)))))))(())(()))())((*()()(((()((()*(())*(()**)()(()).