Correct parentheses (with.Java)
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
s | answer |
---|---|
β()()β | 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 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.