DEV Community

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

Collapse
 
uchitesting profile image
UchiTesting • Edited

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

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