📜  计数删除所有平衡括号子序列所需的对

📅  最后修改于: 2021-09-07 02:21:23             🧑  作者: Mango

给定一个字符串str ,任务是找到需要删除的平衡括号对(str[i], str[j])的最大计数,使得该字符串不包含任何平衡括号的子序列:

例子:

方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:

  • 初始化三个变量,比如cntSqrcntCurlycntSml ,分别存储“[”有效括号的计数、“{}”有效括号的计数和小有效括号的计数。
  • 初始化一个变量,比如cntPairs ,以存储需要删除的平衡括号对的计数,以便字符串不包含任何平衡括号的子序列。
  • 遍历字符串,并检查下列条件的字符:
    • 如果str[i] == ‘(‘ ,则将cntSml的值增加1
    • 如果str[i] = ‘{‘ ,则将cntCurly的值增加1
    • 如果str[i] = ‘[‘ ,则将cntSqr的值增加1
    • 如果str[i] = ‘]’ ,则检查cntSqr 是否大于0 。如果发现为真,则将cntSqr的值减少1并将cntPairs的值增加1
    • 如果str[i] = ‘}’ ,则检查cntCurly 是否大于0 。如果发现为真,则将cntCurly的值减少1并将cntPairs的值增加1
    • 如果str[i] = ‘)’ ,则检查cntSml 是否大于0 。如果发现为真,则将cntSml的值减少1并将cntPairs的值增加1
  • 最后,打印cntPairs的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the maximum count of pairs
// required to be removed such that subsequence
// of string does not contain any valid parenthesis
void cntBalancedParenthesis(string s, int N)
{
 
    // Stores count of pairs
    // of balanced parenthesis
    int cntPairs = 0;
 
    // Stores count of curly
    // balanced parenthesis
    int cntCurly = 0;
 
    // Stores count of small
    // balanced parenthesis
    int cntSml = 0;
 
    // Stores count of square
    // balanced parenthesis
    int cntSqr = 0;
 
    // Iterate over characters
    // of the string
    for (int i = 0; i < N; i++) {
        if (s[i] == '{') {
 
            // Update cntCurly
            cntCurly++;
        }
 
        else if (s[i] == '(') {
 
            // Update cntSml
            cntSml++;
        }
 
        else if (s[i] == '[') {
 
            // Update cntSqr
            cntSqr++;
        }
 
        else if (s[i] == '}' && cntCurly > 0) {
 
            // Update cntCurly
            cntCurly--;
 
            // Update cntPairs
            cntPairs++;
        }
 
        else if (s[i] == ')' && cntSml > 0) {
 
            // Update cntSml
            cntSml--;
 
            // Update cntPairs
            cntPairs++;
        }
 
        else if (s[i] == ']' && cntSqr > 0) {
 
            // Update cntSml
            cntSqr--;
 
            // Update cntPairs
            cntPairs++;
        }
    }
 
    cout << cntPairs;
}
 
// Driver Code
int main()
{
    // Given String
    string s = "{(})";
    int N = s.length();
 
    // Function call
    cntBalancedParenthesis(s, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
class GFG
{
 
  // Function to find the maximum count of pairs
  // required to be removed such that subsequence
  // of string does not contain any valid parenthesis
  static void cntBalancedParenthesis(String s, int N)
  {
 
    // Stores count of pairs
    // of balanced parenthesis
    int cntPairs = 0;
 
    // Stores count of curly
    // balanced parenthesis
    int cntCurly = 0;
 
    // Stores count of small
    // balanced parenthesis
    int cntSml = 0;
 
    // Stores count of square
    // balanced parenthesis
    int cntSqr = 0;
 
    // Iterate over characters
    // of the string
    for (int i = 0; i < N; i++) {
      if (s.charAt(i) == '{')
      {
 
        // Update cntCurly
        cntCurly++;
      }
 
      else if (s.charAt(i) == '(')
      {
 
        // Update cntSml
        cntSml++;
      }
 
      else if (s.charAt(i) == '[')
      {
 
        // Update cntSqr
        cntSqr++;
      }
 
      else if (s.charAt(i) == '}' && cntCurly > 0)
      {
 
        // Update cntCurly
        cntCurly--;
 
        // Update cntPairs
        cntPairs++;
      }
 
      else if (s.charAt(i) == ')' && cntSml > 0)
      {
 
        // Update cntSml
        cntSml--;
 
        // Update cntPairs
        cntPairs++;
      }
 
      else if (s.charAt(i) == ']' && cntSqr > 0)
      {
 
        // Update cntSml
        cntSqr--;
 
        // Update cntPairs
        cntPairs++;
      }
    }
    System.out.println(cntPairs);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given String
    String s = "{(})";
    int N = s.length();
 
    // Function call
    cntBalancedParenthesis(s, N);
  }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program to implement
# the above approach
 
# Function to find the maximum count of pairs
# required to be removed such that subsequence
# of string does not contain any valid parenthesis
def cntBalancedParenthesis(s, N):
   
    # Stores count of pairs
    # of balanced parenthesis
    cntPairs = 0;
 
    # Stores count of curly
    # balanced parenthesis
    cntCurly = 0;
 
    # Stores count of small
    # balanced parenthesis
    cntSml = 0;
 
    # Stores count of square
    # balanced parenthesis
    cntSqr = 0;
 
    # Iterate over characters
    # of the string
    for i in range(N):
        if (ord(s[i]) == ord('{')):
 
            # Update cntCurly
            cntCurly += 1;
        elif (ord(s[i]) == ord('(')):
 
            # Update cntSml
            cntSml += 1;
        elif (ord(s[i]) == ord('[')):
 
            # Update cntSqr
            cntSqr += 1;
        elif (ord(s[i]) == ord('}') and cntCurly > 0):
 
            # Update cntCurly
            cntCurly -=1;
 
            # Update cntPairs
            cntPairs += 1;
        elif (ord(s[i]) == ord(')') and cntSml > 0):
 
            # Update cntSml
            cntSml -=1;
 
            # Update cntPairs
            cntPairs += 1;
        elif (ord(s[i]) == ord(']') and cntSqr > 0):
 
            # Update cntSml
            cntSqr -=1;
 
            # Update cntPairs
            cntPairs += 1;
    print(cntPairs);
 
# Driver Code
if __name__ == '__main__':
   
    # Given String
    s = "{(})";
    N = len(s);
 
    # Function call
    cntBalancedParenthesis(s, N);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
  // Function to find the maximum count of pairs
  // required to be removed such that subsequence
  // of string does not contain any valid parenthesis
  static void cntBalancedParenthesis(String s, int N)
  {
 
    // Stores count of pairs
    // of balanced parenthesis
    int cntPairs = 0;
 
    // Stores count of curly
    // balanced parenthesis
    int cntCurly = 0;
 
    // Stores count of small
    // balanced parenthesis
    int cntSml = 0;
 
    // Stores count of square
    // balanced parenthesis
    int cntSqr = 0;
 
    // Iterate over characters
    // of the string
    for (int i = 0; i < N; i++) {
      if (s[i] == '{') {
 
        // Update cntCurly
        cntCurly++;
      }
 
      else if (s[i] == '(') {
 
        // Update cntSml
        cntSml++;
      }
 
      else if (s[i] == '[') {
 
        // Update cntSqr
        cntSqr++;
      }
 
      else if (s[i] == '}' && cntCurly > 0) {
 
        // Update cntCurly
        cntCurly--;
 
        // Update cntPairs
        cntPairs++;
      }
 
      else if (s[i] == ')' && cntSml > 0) {
 
        // Update cntSml
        cntSml--;
 
        // Update cntPairs
        cntPairs++;
      }
 
      else if (s[i] == ']' && cntSqr > 0) {
 
        // Update cntSml
        cntSqr--;
 
        // Update cntPairs
        cntPairs++;
      }
    }
 
    Console.WriteLine(cntPairs);
  }
 
  // Driver Code
  static public void Main()
  {
 
    // Given String
    String s = "{(})";
    int N = s.Length;
 
    // Function call
    cntBalancedParenthesis(s, N);
  }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
2

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

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