给定一个由‘(‘和‘)’组成的字符串S ,任务是通过重复删除一对不相等的相邻字符来找到使字符串成为空字符串所需的最小括号反转次数。如果无法清空字符串,则打印-1 。
例子:
Input: S = “)))(((“
Output: 0
Explanation:
Removing (S[2], S[3]) from S modifies S to “))((“.
Removing (S[1], S[2]) from S modifies S to “)(“.
Removing (S[0], S[1]) from S modifies S to “”.
Since no flips are required to make S empty.
Therefore, the required output is 0.
Input: S = “))(“
Output: -1
处理方法:按照以下步骤解决问题:
- 将两个整数变量cnt1和cnt2初始化为0 。
- 如果字符串的长度是奇数 或者只存在一种类型的字符,然后打印“-1” 。
- 否则,遍历字符串S的字符,如果当前字符是‘(‘ ,则将cnt1加一。否则,将cnt2加一。
- 完成上述步骤后,打印abs(cnt1 – cnt2)/2 的值作为所需的最小反转次数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum count of steps
// required ot make string S an empty string
void canReduceString(string S, int N)
{
// Stores count of occurences '('
int count_1 = 0;
// Stores count of occurences ')'
int count_2 = 0;
// Traverse the string, str
for (int i = 0; i < N; i++) {
// If current character is '('
if (S[i] == '(') {
// Update count_1
count_1++;
}
else {
// Update count_2
count_2++;
}
}
// If all the characters are
// same, then print -1
if (count_1 == 0 || count_2 == 0) {
cout << "-1" << endl;
}
// If the count of occurence of ')'
// and '(' are same then print 0
else if (count_1 == count_2) {
cout << "0" << endl;
}
// If length of string is Odd
else if (N % 2 != 0) {
cout << "-1";
}
else {
cout << abs(count_1 - count_2) / 2;
}
}
// Driver Code
int main()
{
// Given string
string S = ")))(((";
// Size of the string
int N = S.length();
// Function Call
canReduceString(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum count of steps
// required ot make String S an empty String
static void canReduceString(String S, int N)
{
// Stores count of occurences '('
int count_1 = 0;
// Stores count of occurences ')'
int count_2 = 0;
// Traverse the String, str
for(int i = 0; i < N; i++)
{
// If current character is '('
if (S.charAt(i) == '(')
{
// Update count_1
count_1++;
}
else
{
// Update count_2
count_2++;
}
}
// If all the characters are
// same, then print -1
if (count_1 == 0 || count_2 == 0)
{
System.out.print("-1" + "\n");
}
// If the count of occurence of ')'
// and '(' are same then print 0
else if (count_1 == count_2)
{
System.out.print("0" + "\n");
}
// If length of String is Odd
else if (N % 2 != 0)
{
System.out.print("-1");
}
else
{
System.out.print(Math.abs(
count_1 - count_2) / 2);
}
}
// Driver Code
public static void main(String[] args)
{
// Given String
String S = ")))(((";
// Size of the String
int N = S.length();
// Function Call
canReduceString(S, N);
}
}
// This code is contributed by shikhasingrajput
Python3
#Python3 program for the above approach
# Function to find minimum count of steps
# required ot make string S an empty string
def canReduceString(S, N):
# Stores count of occurences '('
count_1 = 0
# Stores count of occurences ')'
count_2 = 0
# Traverse the string, str
for i in range(N):
# If current character is '('
if (S[i] == '('):
# Update count_1
count_1 += 1
else:
#Update count_2
count_2 += 1
# If all the characters are
# same, then pr-1
if (count_1 == 0 or count_2 == 0):
print("-1")
# If the count of occurence of ')'
# and '(' are same then pr0
elif (count_1 == count_2):
print("0")
# If length of string is Odd
elif (N % 2 != 0):
print("-1")
else:
print (abs(count_1 - count_2) // 2)
# Driver Code
if __name__ == '__main__':
# Given string
S = ")))((("
# Size of the string
N = len(S)
# Function Call
canReduceString(S, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find minimum count of steps
// required ot make String S an empty String
static void canReduceString(String S, int N)
{
// Stores count of occurences '('
int count_1 = 0;
// Stores count of occurences ')'
int count_2 = 0;
// Traverse the String, str
for(int i = 0; i < N; i++)
{
// If current character is '('
if (S[i] == '(')
{
// Update count_1
count_1++;
}
else
{
// Update count_2
count_2++;
}
}
// If all the characters are
// same, then print -1
if (count_1 == 0 || count_2 == 0)
{
Console.Write("-1" + "\n");
}
// If the count of occurence of ')'
// and '(' are same then print 0
else if (count_1 == count_2)
{
Console.Write("0" + "\n");
}
// If length of String is Odd
else if (N % 2 != 0)
{
Console.Write("-1");
}
else
{
Console.Write(Math.Abs(
count_1 - count_2) / 2);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given String
String S = ")))(((";
// Size of the String
int N = S.Length;
// Function Call
canReduceString(S, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
0
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live