给定一个大小为N的平衡字符串str ,其中L和R 的数量相等,任务是找到最大数X ,使得给定的字符串可以被划分为X 个平衡子字符串。如果字符串中‘L’s的数量等于‘R’s的数量,则称为平衡的字符串。
例子:
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 answer 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 answer 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 answer 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 answer 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
Javascript
输出:
4
时间复杂度: O(N)
空间复杂度: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。