01 April 2017

reverseParentheses

My solution:
string reverseParentheses(string s)
{
    string cs = s;
    while (cs.Contains("("))
    {
        string r = Regex.Match(cs, "\\([^\\(\\)]+\\)").Value;
        string r2 = r.Remove(r.Length - 1, 1);
        r2 = r2.Remove(0, 1);
        string r3 = string.Concat(r2.Reverse());
        cs = cs.Replace(r, r3);
    }

    return cs;
}

Previous Next