[LeetCode] 1021. Remove Outermost Parentheses

本文介绍了一种高效的算法,用于解决LeetCode上的移除原始括号串外层括号的问题。通过使用类似栈的数据结构,算法能够准确地定位并移除每个原始括号串的外层括号,最终返回处理后的字符串。

原题链接:https://leetcode.com/problems/remove-outermost-parentheses/

1. Description

A valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation.

For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings.

A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.

Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + … + P_k, where P_i are primitive valid parentheses strings.

Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.

Example 1:

Input: "(()())(())"
Output: "()()()"
Explanation: 
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".

Example 2:

Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation: 
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".

Example 3:

Input: "()()"
Output: ""
Explanation: 
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".

Note:
S.length <= 10000
S[i] is “(” or “)”
S is a valid parentheses string

Every String S is consist of many "primitive valid parentheses string"s. We need to remove every “primitive valid parentheses string”'s outermost parentheses, and then combine them into a new String S. The new String S is this problem’s answer.

2. My Solution

We can use Stacks to solve this problem. However, actually we can also use a Int number to represent the Stack to save time.

initialize a Int st, st is the number of the left parentheses in String S.
When we meet a left parentheses, st ++;
when we meet a right parentheses, st --;
if st == 0, it represents the Stack is empty. When the Stack is empty, we can get a “primitive valid parentheses string”. And then we remove its outermost parentheses and put it back to String.

思路:遍历字符串,遇到左括号就入栈,遇到右括号就出栈,每次栈空的时候,都说明找到了一个原语,记录下每个原语的起始位置和结束位置,取原字符串在原语的起始位置+1到原语的结束位置的子串便得到原语删除了最外层括号的字符串,拼接,即可解出答案。
思路源自: https://leetcode-cn.com/problems/remove-outermost-parentheses/solution/chao-xiang-xi-ti-jie-si-lu-jie-zhu-zhan-yuan-yu-hu/

code

class Solution {
    public String removeOuterParentheses(String S) {
        StringBuilder ans = new StringBuilder();
       
        int start = 0;
        int end = 0;
        boolean flag = false;
        
        // it is a int to motify a stack
        int st = 0;
        
        for(int i = 0 ; i < S.length() ; i++){
            char c = S.charAt(i);
            if(c == '('){                
                //use st++ to replace push
                st++;
                
                //if it is the first '('
                if(flag == false){
                    start = i;
                    flag = true;
                }
                
            }
            else{
                //use st-- to replace pop();
                st --;
                
                //st==0 means that stack.isEmpty() == true
                if(st == 0){
                    end = i;
                    ans.append(S.substring(start +1, end));
                    flag = false;
                }
            }
        }
        return ans.toString();
    }
}

Reference

https://leetcode-cn.com/problems/remove-outermost-parentheses/solution/chao-xiang-xi-ti-jie-si-lu-jie-zhu-zhan-yuan-yu-hu/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值