usingSystem;usingSystem.Text.RegularExpressions;publicclassProgram{publicstaticvoidMain(){strings="RLRRLLRLRL";Console.WriteLine($"Output: {BalancedString(s)}");}// Assuming there can be only 'L' and 'R' in the stringstaticintBalancedString(strings){CheckBalancedString(s);intcount=0;for(inti=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.}}returncount;}staticvoidCheckBalancedString(strings){Regexr=newRegex("^[LR]*$");if(!r.IsMatch(s))thrownewException("Sentence can only be made of 'L' and 'R' characters.");elseConsole.WriteLine($"{s} matches the expression.");}}
I did solve that problem my way with C#.
Here is the code:
I put a link to that .NET Fiddle to play around.