DEV Community

Pankaj Tanwar
Pankaj Tanwar

Posted on • Updated on • Originally published at pankajtanwar.in

Challenge of day #21 - Split a String in Balanced Strings

Hello!

It's day 21 of my coding diary. Yesterday, vercel was having some issues with their build system. Well, it's up and working smooth now. Let's directly move to the problem of the day.

Problem of the day - Split a String in Balanced Strings

Tag - Easy

Balanced strings are those that have an equal quantity of 'L' and 'R' characters.

Given a balanced string s, split it in the maximum amount of balanced strings.

Return the maximum amount of split balanced strings.

Example 1:

Input: s = "RLRRLLRLRL"
Output: 4


This is the worst problem statement, I have ever seen. It's really confusing man, all the example test cases are not clear. I spent time unnecessarily, building some dynamic programming logic for this dumb question.

So, the problem says, to find the maximum amount of split balanced strings. It does not clearly talk about the order of L & R.

Mentioning `splitting gives an idea of considering one sub-string having multiple balanced sub-strings as one only. but still, initially, I got confused with logic for considering all corner cases.

It turned out to be the simplest problem with just tracking count of L & R, that's it.

Here is my solution

`cpp
class Solution {
public:
int balancedStringSplit(string s) {
int res = 0;
int balance = 0;

    for(int i=0;i<s.length();i++) {
        balance += s[i] == 'L' ? 1 : -1;
        if(balance == 0) res++;
    }
    return res;
}
Enter fullscreen mode Exit fullscreen mode

};
`

Not to mention, run time 0ms and faster than 100%. Honestly, I am not happy. Such uncleared problems make me less excited about solving more problems.

Well, I will try some good medium problems tomorrow!


Thanks for being part of my daily-code-workout journey. As always, if you have any thoughts about anything shared above, don't hesitate to reach out.


You might like previous editions of my coding diary

Discussion (2)

Collapse
uchitesting profile image
UchiTesting • Edited on

Hi,
Just talking markdown formatting you should surround snippets with three consecutive backticks. This is why your display is messed up.

ex:

public static void Main(string[] args)
{
   // Your multiline code can be syntax colored.
   // Just surround them with triple backticks and optionally a valid language name
}
Enter fullscreen mode Exit fullscreen mode

AFAIK you cannot syntax color inline code (i.e. code within a sentence). So the most you can do it put that piece of code between single backticks.

ex:

This sentence mention the public keyword as inline code. Just surround keywords or expressions such as foreach (item in collection) in single backticks.

Talking about your challenge, I'm not sure to understand it too much.
Based on the given example I get to that value in another fashion.

Given your string RLRRLLRLRL I would split it that way which also gives 4.

1 - 2 3 4 -
RL R RL LR LR L

Regards.

Collapse
uchitesting profile image
UchiTesting • Edited on

I did solve that problem my way with C#.

Here is the code:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string s = "RLRRLLRLRL";
        Console.WriteLine($"Output: {BalancedString(s)}");
    }

    // Assuming there can be only 'L' and 'R' in the string
    static int BalancedString(string s) {
        CheckBalancedString(s);

        int count = 0;

        for(int i=0; i< s.Length - 1;i++){
            if (!s[i].Equals(s[i+1])) {
                count++;
                i++; // Next index is part of the set hence he hop it.
            }
        }

        return count;
    }

    static void CheckBalancedString(string s){
        Regex r = new Regex("^[LR]*$");
        if (!r.IsMatch(s)) throw new Exception("Sentence can only be made of 'L' and 'R' characters.");
        else Console.WriteLine($"{s} matches the expression.");

    }
}
Enter fullscreen mode Exit fullscreen mode

I put a link to that .NET Fiddle to play around.
Output Screenshot