给定的长度为N的字符串S,仅由开口“(”闭“)”括号的。任务是找到所有索引“ K ”,以使S [K…N-1] + S [0…K-1]为正圆括号。
A regular parentheses string is either empty (“”), “(” + str1 + “)”, or str1 + str2, where str1 and str2 are regular parentheses strings.
For example: “”, “()”, “(())()”, and “(()(()))” are regular parentheses strings.
例子:
Input: str = “)()(”
Output: 2
Explanation:
For K = 1, S = ()(), which is regular.
For K = 3, S = ()(), which is regular.
Input: S = “())(”
Output: 1
Explanation:
For K = 3, S = (()), which is regular.
天真的方法:天真的方法是在每个可能的索引处(例如K )分割给定的字符串str ,并检查str [K,N-1] + str [0,K-1]是否回文。如果是,则打印K的特定值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:这个想法是要观察到,如果在任何一个索引(例如K )处,右括号的数量大于右括号的数量,则该索引就是分割字符串的可能索引。步骤如下:
- 仅当开括号的数量必须等于闭括号的数量时,才可以进行分区。否则,我们无法形成任何分区来平衡括号。
- 创建一个大小为字符串的长度的辅助数组(例如aux [] )。
- 如果在任何索引处(例如i )的字符为‘(’,则遍历给定的字符串,然后将aux [i]更新为1,否则将strong> aux [i]更新为-1。
- 上面的辅助数组中最小元素的频率是将S [K…N-1] + S [0…K-1]变为规则括号字符串所需的分割次数(例如,在索引K处)。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all indices which
// cyclic shift leads to get
// balanced parenthesis
int countCyclicShifts(string& S, int n)
{
int aux[n] = { 0 };
// Create auxiliary array
for (int i = 0; i < n; ++i) {
if (S[i] == '(')
aux[i] = 1;
else
aux[i] = -1;
}
// Finding prefix sum and
// minimum element
int mn = aux[0];
for (int i = 1; i < n; ++i) {
aux[i] += aux[i - 1];
// Update the minimum element
mn = min(mn, aux[i]);
}
// ChecK if count of '(' and
// ')' are equal
if (aux[n - 1] != 0)
return 0;
// Find count of minimum
// element
int count = 0;
// Find the frequency of mn
for (int i = 0; i < n; ++i) {
if (aux[i] == mn)
count++;
}
// Return the count
return count;
}
// Driver Code
int main()
{
// Given string S
string S = ")()(";
int N = S.length();
// Function Call
cout << countCyclicShifts(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find all indices which
// cyclic shift leads to get
// balanced parenthesis
static int countCyclicShifts(String S, int n)
{
// Create auxiliary array
int[] aux = new int[n];
for(int i = 0; i < n; ++i)
{
if (S.charAt(i) == '(')
aux[i] = 1;
else
aux[i] = -1;
}
// Finding prefix sum and
// minimum element
int mn = aux[0];
for(int i = 1; i < n; ++i)
{
aux[i] += aux[i - 1];
// Update the minimum element
mn = Math.min(mn, aux[i]);
}
// Check if count of '(' and ')'
// are equal
if (aux[n - 1] != 0)
return 0;
// Find count of minimum
// element
int count = 0;
// Find the frequency of mn
for(int i = 0; i < n; ++i)
{
if (aux[i] == mn)
count++;
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
// Given string S
String S = ")()(";
// length of the string S
int N = S.length();
System.out.print(countCyclicShifts(S, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find all indices which
# cyclic shift leads to get
# balanced parenthesis
def countCyclicShifts(S, n):
aux = [0 for i in range(n)]
# Create auxiliary array
for i in range(0, n):
if (S[i] == '('):
aux[i] = 1
else:
aux[i] = -1
# Finding prefix sum and
# minimum element
mn = aux[0]
for i in range(1, n):
aux[i] += aux[i - 1]
# Update the minimum element
mn = min(mn, aux[i])
# ChecK if count of '(' and
# ')' are equal
if (aux[n - 1] != 0):
return 0
# Find count of minimum
# element
count = 0
# Find the frequency of mn
for i in range(0, n):
if (aux[i] == mn):
count += 1
# Return the count
return count
# Driver Code
# Given string S
S = ")()("
N = len(S)
# Function call
print(countCyclicShifts(S, N))
# This code is contributed by Sanjit_Prasad
C#
// C# program for the above approach
using System;
class GFG{
// Function to find all indices which
// cyclic shift leads to get
// balanced parenthesis
static int countCyclicShifts(string S, int n)
{
// Create auxiliary array
int[] aux = new int[n];
for(int i = 0; i < n; ++i)
{
if (S[i] == '(')
aux[i] = 1;
else
aux[i] = -1;
}
// Finding prefix sum and
// minimum element
int mn = aux[0];
for(int i = 1; i < n; ++i)
{
aux[i] += aux[i - 1];
// Update the minimum element
mn = Math.Min(mn, aux[i]);
}
// Check if count of '(' and ')'
// are equal
if (aux[n - 1] != 0)
return 0;
// Find count of minimum
// element
int count = 0;
// Find the frequency of mn
for(int i = 0; i < n; ++i)
{
if (aux[i] == mn)
count++;
}
// Return the count
return count;
}
// Driver code
public static void Main(string[] args)
{
// Given string S
string S = ")()(";
// length of the string S
int N = S.Length;
Console.Write(countCyclicShifts(S, N));
}
}
// This code is contributed by rutvik_56
Javascript
2
时间复杂度: O(N) ,其中N是字符串的长度。
辅助空间: O(N) ,其中N是字符串的长度。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。