[Swift]LeetCode1021. 删除最外层的括号 | Remove Outermost Parentheses

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 Bnonempty 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:

  1. S.length <= 10000
  2. S[i] is "(" or ")"
  3. S is a valid parentheses string

有效括号字符串为空 ("")"(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。例如,"""()""(())()" 和 "(()(()))" 都是有效的括号字符串。

如果有效字符串 S 非空,且不存在将其拆分为 S = A+B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。

给出一个非空有效字符串 S,考虑将其进行原语化分解,使得:S = P_1 + P_2 + ... + P_k,其中 P_i 是有效括号字符串原语。

对 S 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S 。

示例 1:

输入:"(()())(())"
输出:"()()()"
解释:
输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。

示例 2:

输入:"(()())(())(()(()))"
输出:"()()()()(())"
解释:
输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
删除每隔部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。

示例 3:

输入:"()()"
输出:""
解释:
输入字符串为 "()()",原语化分解得到 "()" + "()",
删除每个部分中的最外层括号后得到 "" + "" = ""。

提示:

  1. S.length <= 10000
  2. S[i] 为 "(" 或 ")"
  3. S 是一个有效括号字符串

Runtime: 40 ms

Memory Usage: 19.5 MB
 1 class Solution {
 2     func removeOuterParentheses(_ S: String) -> String {
 3         var ret:String = String()
 4         var bal:Int = 0
 5         var cur:String = String()
 6         for c in S
 7         {
 8             if c == "("
 9             {
10                 bal += 1
11             }
12             else
13             {
14                 bal -= 1
15             }
16             cur.append(c)
17             if bal == 0
18             {
19                 ret += cur.subString(1, cur.count - 2)
20                 cur = String()
21             }
22         }
23         return ret    
24     }
25 }
26 
27 extension String {
28     // 截取字符串:指定索引和字符数
29     // - begin: 开始截取处索引
30     // - count: 截取的字符数量
31     func subString(_ begin:Int,_ count:Int) -> String {
32         let start = self.index(self.startIndex, offsetBy: max(0, begin))
33         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
34         return String(self[start..<end]) 
35     }  
36 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10667927.html