📌  相关文章
📜  通过从每个基本子字符串删除最外面的括号来减少字符串

📅  最后修改于: 2021-04-24 21:49:08             🧑  作者: Mango

给定有效括号的字符串S“(”“)”中,任务是打印通过除去选自S原始子串的最外括号中获得的字符串。

例子:

方法:请按照以下步骤解决问题:

  1. 初始化变量计数以存储左括号的数量,即‘(’
  2. 如果count大于0 ,则将每个‘(’添加到结果中,即在遇到原始子字符串的第一个‘(’之后,添加所有‘(’
  3. 如果count大于0 ,则将每个‘)’添加到结果中,即在遇到原始子字符串的最后一个‘)’之前添加所有‘)’
  4. 最后,打印得到的结果字符串。

以下是上述方法的实现-

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)