给定一个括号序列作为字符串str ,任务是查找是否可以通过将最多一个括号从序列中的原始位置移动到任何其他位置来平衡给定的字符串。
例子:
Input: str = “)(()”
Output: Yes
As by moving s[0] to the end will make it valid.
“(())”
Input: str = “()))(()”
Output: No
方法:可以使用本文中讨论的堆栈解决问题。在本文中,将讨论一种不占用额外空间的方法。
如果’(’的频率小于”’的频率。如果上述差异大于1,则序列无法平衡,如果总差异为零,则序列可以平衡。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true if
// the string can be balanced
bool canBeBalanced(string s, int n)
{
// Count to check the difference between
// the frequencies of '(' and ')' and
// count_1 is to find the minimum value
// of freq('(') - freq(')')
int count = 0, count_1 = 0;
// Traverse the given string
for (int i = 0; i < n; i++) {
// Increase the count
if (s[i] == '(')
count++;
// Decrease the count
else
count--;
// Find the minimum value
// of freq('(') - freq(')')
count_1 = min(count_1, count);
}
// If the minimum difference is greater
// than or equal to -1 and the overall
// difference is zero
if (count_1 >= -1 && count == 0)
return true;
return false;
}
// Driver code
int main()
{
string s = "())()(";
int n = s.length();
if (canBeBalanced(s, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to toggle K-th bit of a number N
class GFG
{
// Function that returns true if
// the string can be balanced
static boolean canBeBalanced(String s, int n)
{
// Count to check the difference between
// the frequencies of '(' and ')' and
// count_1 is to find the minimum value
// of freq('(') - freq(')')
int count = 0, count_1 = 0;
// Traverse the given string
for (int i = 0; i < n; i++)
{
// Increase the count
if (s.charAt(i) == '(')
count++;
// Decrease the count
else
count--;
// Find the minimum value
// of freq('(') - freq(')')
count_1 = Math.min(count_1, count);
}
// If the minimum difference is greater
// than or equal to -1 and the overall
// difference is zero
if (count_1 >= -1 && count == 0)
return true;
return false;
}
// Driver code
public static void main(String []args)
{
String s = "())()(";
int n = s.length();
if (canBeBalanced(s, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
# Function that returns true if
# the can be balanced
def canBeBalanced(s, n):
# Count to check the difference between
# the frequencies of '(' and ')' and
# count_1 is to find the minimum value
# of freq('(') - freq(')')
count = 0
count_1 = 0
# Traverse the given string
for i in range(n):
# Increase the count
if (s[i] == '('):
count += 1
# Decrease the count
else:
count -= 1
# Find the minimum value
# of freq('(') - freq(')')
count_1 = min(count_1, count)
# If the minimum difference is greater
# than or equal to -1 and the overall
# difference is zero
if (count_1 >= -1 and count == 0):
return True
return False
# Driver code
s = "())()("
n = len(s)
if (canBeBalanced(s, n)):
print("Yes")
else:
print("No")
# This code is contributed by Mohit Kumar
C#
// C# program to toggle K-th bit of a number N
using System;
class GFG
{
// Function that returns true if
// the string can be balanced
static Boolean canBeBalanced(String s, int n)
{
// Count to check the difference between
// the frequencies of '(' and ')' and
// count_1 is to find the minimum value
// of freq('(') - freq(')')
int count = 0, count_1 = 0;
// Traverse the given string
for (int i = 0; i < n; i++)
{
// Increase the count
if (s[i] == '(')
count++;
// Decrease the count
else
count--;
// Find the minimum value
// of freq('(') - freq(')')
count_1 = Math.Min(count_1, count);
}
// If the minimum difference is greater
// than or equal to -1 and the overall
// difference is zero
if (count_1 >= -1 && count == 0)
return true;
return false;
}
// Driver code
public static void Main(String []args)
{
String s = "())()(";
int n = s.Length;
if (canBeBalanced(s, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
Yes
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。