Continue to create, accelerate growth! This is the 7th day of my participation in the "Nuggets Daily New Plan · October Update Challenge", click to view the details of the event
1. The topic
Bracketed strings are valid only if one of the following is true:
- it is an empty string, or
- It can be written
AB
(A
andB
concatenated), whereA
andB
are both valid strings, or- It can be written
(A)
whereA
is a valid string.
Given a bracketed string s
, move it N times and you can insert a bracket anywhere in the string.
For example, if s = "()))"
, you can insert an opening bracket as "(()))"
or closing bracket as "())))"
.
Returns the minimum number of parentheses that must be added to make the resulting string s
valid .
2. Examples
2.1> Example 1:
【Input】s = "())"
【Output】1
2.2> Example 2:
【Input】s = "((("
【Output】3
hint:
1
<= s.length <=1000
s
Contains only'('
and')'
characters.
Three, problem-solving ideas
这道题的题目描述真的挺让人费解的。其实题目的意思就是,我们如果想要配对好所有的括号,需要在原有字符串s
的基础上,添加多少个括号(可能是左括号、也可能是右括号)。那么针对于这种配对类型类型的题目,第一个想法就是使用堆栈来实现。当然,对于括号配对
的特殊性,即:左括号 + 右括号 。我们也可以根据这个规律去计算。如下是两种解题算法的详细解释。
3.1> 思路1:利用栈特性去计算
我们可以通过对字符串s
进行每个字符的遍历,放到堆栈中。当发现栈顶字符是‘(’
,待入栈的字符是‘)’
,则符合括号匹配的情况。那么,此时我们只需将栈顶字符出栈即可。而针对于其他情况,我们都是将遍历的字符入栈即可。那么字符串s遍历完毕之后,我们来调用size()
方法计算存储的字符长度,返回的长度就是这道题的结果。具体逻辑如下图所示:
针对该思路的代码实现,请参见:代码实现 4.1> 利用栈特性去计算
3.2> 思路2:找规律去匹配
The [hint] of the passing question s
only contains '('
and ')'
characters. Therefore, for the matching of two characters, there are four cases as shown in the figure below. Then, only [case 1] will match successfully , and other cases will fail to match. Then we create two variables: leftCount
(number of left parentheses) and rightCount
(number of right parentheses).
- If the traversed character is a left parenthesis , execute
leftCount++
- If the traversed character is a closing bracket and leftCount is not 0 , execute
leftCount--
- If the traversed character is a closing bracket and leftCount is equal to 0 , execute
rightCount++
For the code implementation of this idea, please refer to: Code Implementation 4.2> Find the rules to match
Fourth, code implementation
4.1> Implementation 1: Use the stack feature to calculate
class Solution {
public int minAddToMakeValid(String s) {
Deque<Character> deque = new ArrayDeque();
for (char sc : s.toCharArray()) {
if (deque.size() != 0 && (deque.peekLast()).equals('(') && sc == ')') deque.removeLast();
else deque.addLast(sc);
}
return deque.size();
}
}
复制代码
4.2> Realization 2: Find rules to match
class Solution {
public int minAddToMakeValid(String s) {
int leftCount = 0, rightCount = 0;
for (char item : s.toCharArray()) {
if (item == '(') {
leftCount++;
} else {
if (leftCount == 0) rightCount++;
else leftCount--;
}
}
return leftCount + rightCount;
}
}
复制代码
That's all for today's article:
Writing is not easy. An article completed in a few hours or even days by the author will only be exchanged for a few seconds of likes & shares from you .
For more technical dry goods, you are welcome to pay attention to the public account " Java Muse " ~ \(^o^)/ ~ "Dry goods sharing, updated every day"