给定大小为N且大小等于L和R的平衡字符串str ,任务是找到最大数X ,以便可以将给定的字符串划分为X个平衡子字符串。如果字符串的“ L ”数量等于“ R”的数量,则称为平衡字符串。
例子:
Input : str = “LRLLRRLRRL”
Output : 4
Explanation: { “LR”, “LLRR”, “LR”, “RL”} are the possible partitions.
Input : “LRRRRLLRLLRL”
Output : 3
Explanation: {“LR”, “RRRLLRLL”, “RL”} are the possible partitions.
方法:解决此问题的方法是遍历字符串,并在遇到任何情况时保持L和R的计数递增。当L和R的相应计数相等时,将立即形成平衡的括号。因此,此类实例的数量给出了所需的最大可能分区。
下面是上述方法的实现:
C++
// C++ program to find a maximum number X, such
// that a given string can be partitioned
// into X substrings that are each balanced
#include
using namespace std;
// Function to find a maximum number X, such
// that a given string can be partitioned
// into X substrings that are each balanced
int BalancedPartition(string str, int n)
{
// If the size of the string is 0,
// then anwer is zero
if (n == 0)
return 0;
// variable that represents the
// number of 'R's and 'L's
int r = 0, l = 0;
// To store maximum number of
// possible partitions
int ans = 0;
for (int i = 0; i < n; i++) {
// increment the variable r if the
// character in the string is 'R'
if (str[i] == 'R') {
r++;
}
// increment the variable l if the
// character in the string is 'L'
else if (str[i] = 'L') {
l++;
}
// if r and l are equal,
// then increment ans
if (r == l) {
ans++;
}
}
// Return the required answer
return ans;
}
// Driver code
int main()
{
string str = "LLRRRLLRRL";
int n = str.size();
// Function call
cout << BalancedPartition(str, n) << endl;
return 0;
}
Java
// Java program to find a maximum number X, such
// that a given String can be partitioned
// into X subStrings that are each balanced
import java.util.*;
class GFG{
// Function to find a maximum number X, such
// that a given String can be partitioned
// into X subStrings that are each balanced
static int BalancedPartition(String str, int n)
{
// If the size of the String is 0,
// then anwer is zero
if (n == 0)
return 0;
// Variable that represents the
// number of 'R's and 'L's
int r = 0, l = 0;
// To store maximum number of
// possible partitions
int ans = 0;
for(int i = 0; i < n; i++)
{
// Increment the variable r if the
// character in the String is 'R'
if (str.charAt(i) == 'R')
{
r++;
}
// Increment the variable l if the
// character in the String is 'L'
else if (str.charAt(i) == 'L')
{
l++;
}
// If r and l are equal,
// then increment ans
if (r == l)
{
ans++;
}
}
// Return the required answer
return ans;
}
// Driver code
public static void main(String[] args)
{
String str = "LLRRRLLRRL";
int n = str.length();
// Function call
System.out.print(BalancedPartition(str, n) + "\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find a maximum number X,
# such that a given string can be partitioned
# into X substrings that are each balanced
# Function to find a maximum number X, such
# that a given string can be partitioned
# into X substrings that are each balanced
def BalancedPartition(str1, n):
# If the size of the string is 0,
# then anwer is zero
if (n == 0):
return 0
# Variable that represents the
# number of 'R's and 'L's
r = 0
l = 0
# To store maximum number of
# possible partitions
ans = 0
for i in range(n):
# Increment the variable r if the
# character in the string is 'R'
if (str1[i] == 'R'):
r += 1
# Increment the variable l if the
# character in the string is 'L'
elif (str1[i] == 'L'):
l += 1
# If r and l are equal,
# then increment ans
if (r == l):
ans += 1
# Return the required answer
return ans
# Driver code
if __name__ == '__main__':
str1 = "LLRRRLLRRL"
n = len(str1)
# Function call
print(BalancedPartition(str1, n))
# This code is contributed by Bhupendra_Singh
C#
// C# program to find a maximum number X, such
// that a given String can be partitioned
// into X subStrings that are each balanced
using System;
class GFG{
// Function to find a maximum number X, such
// that a given String can be partitioned
// into X subStrings that are each balanced
static int BalancedPartition(string str, int n)
{
// If the size of the String is 0,
// then anwer is zero
if (n == 0)
return 0;
// Variable that represents the
// number of 'R's and 'L's
int r = 0, l = 0;
// To store maximum number of
// possible partitions
int ans = 0;
for(int i = 0; i < n; i++)
{
// Increment the variable r if the
// character in the String is 'R'
if (str[i] == 'R')
{
r++;
}
// Increment the variable l if the
// character in the String is 'L'
else if (str[i] == 'L')
{
l++;
}
// If r and l are equal,
// then increment ans
if (r == l)
{
ans++;
}
}
// Return the required answer
return ans;
}
// Driver code
public static void Main()
{
string str = "LLRRRLLRRL";
int n = str.Length;
// Function call
Console.Write(BalancedPartition(str, n) + "\n");
}
}
// This code is contributed by Nidhi_Biet
输出:
4
时间复杂度: O(N)
空间复杂度: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。