📌  相关文章
📜  通过从给定字符串删除形成有效括号的子序列来最小化长度

📅  最后修改于: 2021-09-06 06:17:00             🧑  作者: Mango

给定一个由‘(‘, ‘)’, ‘[‘‘]’组成的字符串S ,任务是通过删除有效括号的子序列来找到字符串中剩余字符的最小计数。

例子:

方法:使用Stack可以解决这个问题。请按照以下步骤解决问题:

  • 这个想法是在两个单独的堆栈中处理圆括号“()”和方括号“[]”
  • 初始化两个变量,比如 roundCountsquareCount,以分别将括号的计数存储在‘()’‘[]’ 的有效括号中。
  • 迭代给定字符串的每个字符,并使用两个不同的堆栈计算‘()’‘[]’的有效括号的长度。
  • 最后,打印(N – 2 * (roundCount + squareCount)) 的值

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum count of remaining
// characters left into the string by removing
// the valid subsequences
void deleteSubseq(string s)
{
 
    // Length of the string
    int N = s.size();
 
    // Stores opening parenthesis
    // '(' of the given string
    stack roundStk;
 
    // Stores square parenthesis
    // '[' of the given string
    stack squareStk;
 
    // Stores count of opening parenthesis '('
    // in valid subsequences
    int roundCount = 0;
 
    // Stores count of opening parenthesis '['
    // in valid subsequences
    int squareCount = 0;
 
    // Iterate over each
    // characters of S
    for (int i = 0; i < N; i++)
    {
 
        // If current character is '['
        if (s[i] == '[')
        {
 
            // insert into stack
            squareStk.push(s[i]);
        }
 
        // If i is equal to ']'
        else if (s[i] == ']')
        {
 
            // If stack is not empty and
            // top element of stack is '['
            if (squareStk.size() != 0
                && squareStk.top() == '[')
            {
 
                // Remove top element from stack
                squareStk.pop();
 
                // Update squareCount
                squareCount += 1;
            }
        }
 
        // If current character is '('
        else if (s[i] == '(')
        {
 
            // Insert into stack
            roundStk.push(s[i]);
        }
 
        // If i is equal to ')'
        else
        {
 
            // If stack is not empty and
            // top element of stack is '('
            if (roundStk.size() != 0
                && squareStk.top() == '(')
            {
 
                // Remove top element from stack
                squareStk.pop();
 
                // Update roundCount
                roundCount += 1;
            }
        }
    }
 
    // Print the minimum number of remaining
    // characters left into S
    cout << (N - (2 * squareCount + 2 * roundCount));
}
 
// Driver code
int main()
{
 
    // input string
    string s = "[]])([";
 
    // function call
    deleteSubseq(s);
}
 
// This code is contributed by gauravrajput1


Java
/*package whatever //do not write package name here */
 
// Java program for the above approach
import java.io.*;
import java.util.Stack;
class GFG
{
 
  // Function to find the minimum count of remaining
  // characters left into the string by removing
  // the valid subsequences
  public static void deleteSubseq(String s)
  {
 
    // Length of the string
    int N = s.length();
 
    // Stores opening parenthesis
    // '(' of the given string
    Stack roundStk = new Stack<>();
 
    // Stores square parenthesis
    // '[' of the given string
    Stack squareStk = new Stack<>();
 
    // Stores count of opening parenthesis '('
    // in valid subsequences
    int roundCount = 0;
 
    // Stores count of opening parenthesis '['
    // in valid subsequences
    int squareCount = 0;
 
    // Iterate over each
    // characters of S
    for (int i = 0; i < N; i++)
    {
 
      // If current character is '['
      if (s.charAt(i) == '[')
      {
 
        // insert into stack
        squareStk.push(s.charAt(i));
      }
 
      // If i is equal to ']'
      else if (s.charAt(i) == ']')
      {
 
        // If stack is not empty and
        // top element of stack is '['
        if (squareStk.size() != 0
            && squareStk.peek() == '[')
        {
 
          // Remove top element from stack
          squareStk.pop();
 
          // Update squareCount
          squareCount += 1;
        }
      }
 
      // If current character is '('
      else if (s.charAt(i) == '(')
      {
 
        // Insert into stack
        roundStk.push(s.charAt(i));
      }
 
      // If i is equal to ')'
      else
      {
 
        // If stack is not empty and
        // top element of stack is '('
        if (roundStk.size() != 0
            && squareStk.peek() == '(')
        {
 
          // Remove top element from stack
          squareStk.pop();
 
          // Update roundCount
          roundCount += 1;
        }
      }
    }
 
    // Print the minimum number of remaining
    // characters left into S
    System.out.println(
      N - (2 * squareCount + 2 * roundCount));
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // input string
    String s = "[]])([";
 
    // function call
    deleteSubseq(s);
  }
}
 
// This code is contributed by aditya7409


Python3
# Python program for the above approach
 
# Function to find the minimum count of remaining
# characters left into the string by removing
# the valid subsequences
def deleteSubseq(S):
 
    # Length of the string
    N = len(S)
 
    # Stores opening parenthesis
    # '(' of the given string
    roundStk = []
 
    # Stores square parenthesis
    # '[' of the given string
    squareStk = []
 
    # Stores count of opening parenthesis '('
    # in valid subsequences
    roundCount = 0
 
    # Stores count of opening parenthesis '['
    # in valid subsequences
    squareCount = 0
 
    # Iterate over each
    # characters of S
    for i in S:
 
        # If current character is '['
        if i == '[':
 
            # Insert into stack
            squareStk.append(i)
 
        # If i is equal to ']'
        elif i == ']':
             
             
            # If stack is not empty and
            # top element of stack is '['
            if squareStk and squareStk[-1] == '[':
                 
                 
                # Remove top element from stack
                squareStk.pop()
                 
                 
                # Update squareCount
                squareCount += 1
 
        # If current character is '('
        elif i == '(':
             
             
            # Insert into stack
            roundStk.append(i)
        else:
             
             
            # If stack is not empty and
            # top element of stack is '('
            if roundStk and roundStk[-1] == '(':
                 
                 
                # Remove top element from stack
                roundStk.pop()
                 
                 
                # Update roundCount
                roundCount += 1
 
 
    # Print the minimum number of remaining
    # characters left into S
    print(N - (2 * squareCount + 2 * roundCount))
 
 
# Driver Code
if __name__ == '__main__':
     
    # Given string
    S = '[]])(['
 
    # Function Call
    deleteSubseq(S)


C#
/*package whatever //do not write package name here */
 
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to find the minimum count of remaining
  // characters left into the string by removing
  // the valid subsequences
  public static void deleteSubseq(String s)
  {
 
    // Length of the string
    int N = s.Length;
 
    // Stores opening parenthesis
    // '(' of the given string
    Stack roundStk = new Stack();
 
    // Stores square parenthesis
    // '[' of the given string
    Stack squareStk = new Stack();
 
    // Stores count of opening parenthesis '('
    // in valid subsequences
    int roundCount = 0;
 
    // Stores count of opening parenthesis '['
    // in valid subsequences
    int squareCount = 0;
 
    // Iterate over each
    // characters of S
    for (int i = 0; i < N; i++)
    {
 
      // If current character is '['
      if (s[i] == '[')
      {
 
        // insert into stack
        squareStk.Push(s[i]);
      }
 
      // If i is equal to ']'
      else if (s[i] == ']')
      {
 
        // If stack is not empty and
        // top element of stack is '['
        if (squareStk.Count != 0
            && squareStk.Peek() == '[')
        {
 
          // Remove top element from stack
          squareStk.Pop();
 
          // Update squareCount
          squareCount += 1;
        }
      }
 
      // If current character is '('
      else if (s[i] == '(')
      {
 
        // Insert into stack
        roundStk.Push(s[i]);
      }
 
      // If i is equal to ')'
      else
      {
 
        // If stack is not empty and
        // top element of stack is '('
        if (roundStk.Count != 0
            && squareStk.Peek() == '(')
        {
 
          // Remove top element from stack
          squareStk.Pop();
 
          // Update roundCount
          roundCount += 1;
        }
      }
    }
 
    // Print the minimum number of remaining
    // characters left into S
    Console.WriteLine(
      N - (2 * squareCount + 2 * roundCount));
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    // input string
    String s = "[]])([";
 
    // function call
    deleteSubseq(s);
  }
}
 
// This code is contributed by 29AjayKumar


输出:
4

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live