交换和删除字符后平衡字符串的最大长度
给定一个字符串str仅由字符'(' , ')' , '[' , ']' , '{'和'}'组成。任务是在删除任何字符并交换任何两个相邻字符后找到平衡字符串的最大长度。
例子:
Input: str = “))[]]((”
Output: 6
The string can be converted to ()[]()
Input: str = “{{{{{{{}”
Output: 2
方法:这个想法是从字符串中删除额外的不匹配括号,因为我们无法为它生成一个平衡的对并交换剩余的字符来平衡字符串。因此,答案是所有平衡括号对的相等总和。请注意,我们可以通过相邻交换将字符移动到任何其他位置。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the length of
// the longest balanced sub-string
int maxBalancedStr(string s)
{
// To store the count of parentheses
int open1 = 0, close1 = 0;
int open2 = 0, close2 = 0;
int open3 = 0, close3 = 0;
// Traversing the string
for (int i = 0; i < s.length(); i++) {
// Check type of parentheses and
// incrementing count for it
switch (s[i]) {
case '(':
open1++;
break;
case ')':
close1++;
break;
case '{':
open2++;
break;
case '}':
close2++;
break;
case '[':
open3++;
break;
case ']':
close3++;
break;
}
}
// Sum all pair of balanced parentheses
int maxLen = 2 * min(open1, close1)
+ 2 * min(open2, close2)
+ 2 * min(open3, close3);
return maxLen;
}
// Driven code
int main()
{
string s = "))[]]((";
cout << maxBalancedStr(s);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the length of
// the longest balanced sub-string
static int maxBalancedStr(String s)
{
// To store the count of parentheses
int open1 = 0, close1 = 0;
int open2 = 0, close2 = 0;
int open3 = 0, close3 = 0;
// Traversing the string
for (int i = 0; i < s.length(); i++)
{
// Check type of parentheses and
// incrementing count for it
switch (s.charAt(i))
{
case '(':
open1++;
break;
case ')':
close1++;
break;
case '{':
open2++;
break;
case '}':
close2++;
break;
case '[':
open3++;
break;
case ']':
close3++;
break;
}
}
// Sum all pair of balanced parentheses
int maxLen = 2 * Math.min(open1, close1)
+ 2 * Math.min(open2, close2)
+ 2 * Math.min(open3, close3);
return maxLen;
}
// Driven code
public static void main(String[] args)
{
String s = "))[]]((";
System.out.println(maxBalancedStr(s));
}
}
// This code is contributed by Code_Mech.
Python3
# Python 3 implementation of the approach
# Function to return the length of
# the longest balanced sub-string
def maxBalancedStr(s):
# To store the count of parentheses
open1 = 0
close1 = 0
open2 = 0
close2 = 0
open3 = 0
close3 = 0
# Traversing the string
for i in range(len(s)):
# Check type of parentheses and
# incrementing count for it
if(s[i] == '('):
open1 += 1
continue
if s[i] == ')':
close1 += 1
continue
if s[i] == '{':
open2 += 1
continue
if s[i] == '}':
close2 += 1
continue
if s[i] == '[':
open3 += 1
continue
if s[i] == ']':
close3 += 1
continue
# Sum all pair of balanced parentheses
maxLen = (2 * min(open1, close1) +
2 * min(open2, close2) +
2 * min(open3, close3))
return maxLen
# Driven code
if __name__ == '__main__':
s = "))[]](("
print(maxBalancedStr(s))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the length of
// the longest balanced sub-string
static int maxBalancedStr(string s)
{
// To store the count of parentheses
int open1 = 0, close1 = 0;
int open2 = 0, close2 = 0;
int open3 = 0, close3 = 0;
// Traversing the string
for (int i = 0; i < s.Length; i++)
{
// Check type of parentheses and
// incrementing count for it
switch (s[i])
{
case '(':
open1++;
break;
case ')':
close1++;
break;
case '{':
open2++;
break;
case '}':
close2++;
break;
case '[':
open3++;
break;
case ']':
close3++;
break;
}
}
// Sum all pair of balanced parentheses
int maxLen = 2 * Math.Min(open1, close1)
+ 2 * Math.Min(open2, close2)
+ 2 * Math.Min(open3, close3);
return maxLen;
}
// Driver code
public static void Main()
{
string s = "))[]]((";
Console.WriteLine(maxBalancedStr(s));
}
}
// This code is contributed by Code_Mech.
PHP
Javascript
输出:
6