给定有效括号的字符串S“(”和“)”中,任务是打印通过除去选自S每原始子串的最外括号中获得的字符串。
A valid parentheses substring S is primitive if it is non-empty, and cannot be split into two or more non-empty substrings which are also a valid parentheses.
例子:
Input: S = “(()())(())()”
Output: ()()()
Explanation: The input string is “(()())(())()” can be decomposed into primitive susbstrings “(()())” + “(())”+”()”. After removing outermost parentheses of each priiimitive substrings, the string obtained is “()()” + “()” = “()()()”
Input: S = “((()())(())(()(())))”
Output: ()()()()(())
方法:请按照以下步骤解决问题:
- 初始化变量计数以存储左括号的数量,即‘(’ 。
- 如果count大于0 ,则将每个‘(’添加到结果中,即在遇到原始子字符串的第一个‘(’之后,添加所有‘(’ 。
- 如果count大于0 ,则将每个‘)’添加到结果中,即在遇到原始子字符串的最后一个‘)’之前添加所有‘)’ 。
- 最后,打印得到的结果字符串。
以下是上述方法的实现-
C++
// C++ program to implement the
// above approach
#include
using namespace std;
// Function to remove the outermost
// parentheses of every primitive
// substring from the given string
string removeOuterParentheses(string S)
{
// Stores the resultant string
string res;
// Stores the count of
// opened parentheses
int count = 0;
// Traverse the string
for (char c : S) {
// If opening parenthesis is
// encountered and their
// count exceeds 0
if (c == '(' && count++ > 0)
// Include the character
res += c;
// If closing parenthesis is
// encountered and their
// count is less than count
// of opening parentheses
if (c == ')' && count-- > 1)
// Include the character
res += c;
}
// Return the resultant string
return res;
}
// Driver Code
int main()
{
string S = "(()())(())()";
cout << removeOuterParentheses(S);
}
Java
// Java program to implement the
// above approach
import java.io.*;
class GFG{
// Function to remove the outermost
// parentheses of every primitive
// substring from the given string
static String removeOuterParentheses(String S)
{
// Stores the resultant
// string
String res = "";
// Stores the count of
// opened parentheses
int count = 0;
// Traverse the string
for (int c = 0;
c < S.length(); c++)
{
// If opening parenthesis is
// encountered and their
// count exceeds 0
if (S.charAt(c) == '(' &&
count++ > 0)
// Include the character
res += S.charAt(c);
// If closing parenthesis is
// encountered and their
// count is less than count
// of opening parentheses
if (S.charAt(c) == ')' &&
count-- > 1)
// Include the character
res += S.charAt(c);
}
// Return the resultant string
return res;
}
// Driver Code
public static void main(String[] args)
{
String S = "(()())(())()";
System.out.print(removeOuterParentheses(S));
}
}
// This code is contributed by Chitranayal
Python3
# Python3 program to implement the
# above approach
# Function to remove the outermost
# parentheses of every primitive
# substring from the given string
def removeOuterParentheses(S):
# Stores the resultant string
res = ""
# Stores the count of
# opened parentheses
count = 0
# Traverse the string
for c in S:
# If opening parenthesis is
# encountered and their
# count exceeds 0
if (c == '(' and count > 0):
# Include the character
res += c
# If closing parenthesis is
# encountered and their
# count is less than count
# of opening parentheses
if (c == '('):
count += 1
if (c == ')' and count > 1):
# Include the character
res += c
if (c == ')'):
count -= 1
# Return the resultant string
return res
# Driver Code
if __name__ == '__main__':
S = "(()())(())()"
print(removeOuterParentheses(S))
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to remove the outermost
// parentheses of every primitive
// substring from the given string
static string removeOuterParentheses(string S)
{
// Stores the resultant
// string
string res = "";
// Stores the count of
// opened parentheses
int count = 0;
// Traverse the string
for(int c = 0; c < S.Length; c++)
{
// If opening parenthesis is
// encountered and their
// count exceeds 0
if (S == '(' &&
count++ > 0)
// Include the character
res += S;
// If closing parenthesis is
// encountered and their
// count is less than count
// of opening parentheses
if (S == ')' &&
count-- > 1)
// Include the character
res += S;
}
// Return the resultant string
return res;
}
// Driver Code
public static void Main()
{
string S = "(()())(())()";
Console.Write(removeOuterParentheses(S));
}
}
// This code is contributed by sanjoy_62
输出:
()()()
时间复杂度: O(N)
辅助空间: O(N)