Contents

Correct parentheses (with.Java)

   Sep 23, 2024     3 min read

Correct parentheses (with.J)

I’ve learned about the correct parentheses (with.Java).

I want to solve the coding test problem, find out how to solve it differently from the retrospective of the problem I solved, and get to know.

Let’s get to the problem first.

Problem

Parentheses correctly paired means that if opened with a β€˜(β€˜ character, it must be paired and closed with a β€˜β€™) character, for example

"(()" or "((())))()" is the correct parentheses.
"()()" or "(()()" is an invalid parentheses.
Given a string s consisting of only '(' or '), complete the solution function that returns true if the string s is correct parentheses and false if it is incorrect parentheses.

Restrictions

  • Length of string s: natural numbers no more than 100,000
  • String s consists of only β€˜(β€˜ or β€˜β€™)’.

Input/output Examples

sanswer
”()()”true
”(())()”true
”)()(β€œfalse
”(()(β€œfalse

problem solving

import java.util.ArrayDeque;
import java.util.Deque;

class Solution {
    boolean solution(String s) {
        Deque<Character> deque = new ArrayDeque<>();
        for(int i = 0; i < s.length(); i++){
            deque.offer(s.charAt(i));
        }

        int count = 0;
        int size = deque.size();
        if(deque.peek() == ')' || deque.peekLast() == '('){
            return false;
        }else{
            while(!deque.isEmpty()){
                if(count < 0 || count > deque.size()){
                    return false;
                }else{
                    if(deque.poll() == '('){
                        count++;
                    }else{
                        count--;
                    }
                }
            }
        }

        return true;
    }
}

Solution Description

This code is to verify that the string s is a valid parentheses string.

A valid parentheses string must appear before the opening parentheses β€˜(β€˜ always closing parentheses β€˜), and the number of opening parentheses must be the same as the number of closing parentheses.

The code uses Deque to process strings and validates parentheses.

Below is a pool review that describes each part of the code.

Deque initialization and string storage:

Deque deque = new ArrayDeque<>(); Save each character in string s to Deque.

Deque is a bidirectional queue that is advantageous for character insertion and deletion.

Add string characters to Deque:

for (int i = 0; i < s.length(); i++) { deque.offer(s.charAt(i)); } Prepare to process sequentially by adding each character in the string to Deque.

Initial condition check:

if (deque.peek() == β€˜)’ || deque.peekLast() == β€˜(β€˜) { return false; } If the first character of the Deque is β€˜β€™) or the last character is β€˜(), return false immediately because it cannot be a valid parentheses string.

Validation of parentheses:

while (!deque.isEmpty()) { … } Repeat until Deque begs.

Use the count variable to balance the parentheses that you open with the parentheses that you close.

The opening parentheses β€˜(β€˜ increase the count, and the closing parentheses β€˜β€™) decrease the count.

Balance check:

if (count < 0 || count > deque.size()) { return false; } If the count is less than 0 or greater than the size of the remaining Deque, it is not a valid parentheses string.

Returns false if there are too many parentheses to open or too many parentheses to close.

Last return value:

After processing all characters, if the count is 0, return true because it is a valid parentheses string; otherwise, return false.

Problems and Improvements

Initial condition check: Instead of using Deque to save the string and check the condition, you can check the string immediately.