📜  在任何指数上平衡给定括号的最小掉期

📅  最后修改于: 2022-05-13 01:57:08.603000             🧑  作者: Mango

在任何指数上平衡给定括号的最小掉期

给定一个由相等数量的左括号 '[' 和右括号 ']' 组成的平衡字符串,计算使字符串平衡的最小交换次数。不平衡的字符串可以通过交换任意两个括号来平衡。

例子:

方法:给定的问题可以通过遍历字符串并按照以下步骤来解决:

  • 所有平衡的括号都被删除,因为它们不需要任何交换来平衡字符串
  • 由于左括号'['和右括号的数量相同']',删除平衡组件后,剩余字符串变为] ] ]…..[
  • 最佳方法是在一次交换中平衡两组括号
  • 对于每两对方括号,交换将使它们平衡。
  • 如果不平衡对的数量是奇数,则需要再进行一次交换。
  • 如果p是不平衡对的数量,那么

下面是上述方法的实现

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to balance the given bracket by swap
int BalancedStringBySwapping(string s)
{
 
    // To count the number of uunbalanced pairs
    int unbalancedPair = 0;
    for (int i = 0; i < s.length(); ++i)
    {
 
        // if there is an opening bracket and
        // we encounter closing bracket then it will
        // decrement the count of unbalanced bracket.
        if (unbalancedPair > 0 && s[i] == ']')
        {
            --unbalancedPair;
        }
 
        // else it will increment unbalanced pair count
        else if (s[i] == '[')
        {
            ++unbalancedPair;
        }
    }
 
    return (unbalancedPair + 1) / 2;
}
 
// Driver code
int main()
{
 
    string s = "]]][[[";
    cout << (BalancedStringBySwapping(s));
 
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java implementation for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
// Function to balance the given bracket by swap
    static int BalancedStringBySwapping(String s)
    {
 
        // To count the number of uunbalanced pairs
        int unbalancedPair = 0;
        for (int i = 0; i < s.length(); ++i) {
 
            // if there is an opening bracket and
            // we encounter closing bracket then it will
            // decrement the count of unbalanced bracket.
            if (unbalancedPair > 0 && s.charAt(i) == ']') {
                --unbalancedPair;
            }
 
            // else it will increment unbalanced pair count
            else if (s.charAt(i) == '[') {
                ++unbalancedPair;
            }
        }
 
        return (unbalancedPair + 1) / 2;
    }
 
// Driver code
    public static void main(String[] args)
    {
 
        String s = "]]][[[";
        System.out.println(BalancedStringBySwapping(s));
    }
 
}


Python3
# Python3 implementation for the above approach
 
# Function to balance the given bracket by swap
def BalancedStringBySwapping(s) :
 
    # To count the number of uunbalanced pairs
    unbalancedPair = 0;
    for i in range(len(s)) :
 
        # if there is an opening bracket and
        # we encounter closing bracket then it will
        # decrement the count of unbalanced bracket.
        if (unbalancedPair > 0 and s[i] == ']') :
         
            unbalancedPair -= 1;
 
        # else it will increment unbalanced pair count
        elif (s[i] == '[') :
         
            unbalancedPair += 1;
 
    return (unbalancedPair + 1) // 2;
 
# Driver code
if __name__ == "__main__" :
 
    s = "]]][[[";
    print(BalancedStringBySwapping(s));
 
 
    # This code is contributed by AnkThon.


C#
// C# implementation for the above approach
using System;
 
class GFG
{
 
  // Function to balance the given bracket by swap
  static int BalancedStringBySwapping(String s)
  {
 
    // To count the number of uunbalanced pairs
    int unbalancedPair = 0;
    for (int i = 0; i < s.Length; ++i) {
 
      // if there is an opening bracket and
      // we encounter closing bracket then it will
      // decrement the count of unbalanced bracket.
      if (unbalancedPair > 0 && s[i] == ']') {
        --unbalancedPair;
      }
 
      // else it will increment unbalanced pair count
      else if (s[i] == '[') {
        ++unbalancedPair;
      }
    }
 
    return (unbalancedPair + 1) / 2;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    String s = "]]][[[";
    Console.Write(BalancedStringBySwapping(s));
  }
 
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
2

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