Maximum Balanced String Partitions

Given a balanced string str of size N with an equal number of L and R, the task is to find a maximum number X, such that a given string can be partitioned into X balanced substring. A string called to be balanced if the number of ‘L’s in the string equals the number of ‘R’s.

Examples:

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.

Approach: The approach to solving this problem is to loop through the string and keep incrementing the count of L and R whenever encountered. Any instant when the respective counts of L and R become equal, a balanced parenthesis is formed. Thus the count of such instances gives the desired maximum possible partitions.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find a maximum number X, such
// that a given string can be partitioned
// into X substrings that are each balanced
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

4

Time Complexity: O(N)
Space Complexity: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.