01 April 2017

Are Similar?

My solution:
bool areSimilar(int[] A, int[] B) 
{
    if (A.Where((t, i) => t != B[i]).Count() > 2)
        return false;

    return A.Distinct().OrderBy(x => x).SequenceEqual(B.Distinct().OrderBy(x => x).ToArray());
}

Previous Next

Add Border

My solution:
string[] addBorder(string[] picture)
{
    return new[] { new string('*', picture.Max(x => x.Length) + 2) }.Concat(picture.Select(x => "*" + x + "*")).Concat(new[] { new string('*', picture.Max(x => x.Length) + 2) }).ToArray();
}

Previous Next

alternatingSums

My solution:
int[] alternatingSums(int[] a)
{
    return new[] { a.Where((b, c) => c % 2 == 0).Sum(), a.Where((b, c) => c % 2 == 1).Sum() };
}

Previous Next

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

Sort by Height

My solution:
int[] sortByHeight(int[] a)
{
    int b = 0;
    return a.Select(c => c == -1 ? c : a.Where(d => d != -1).OrderBy(d => d).ToArray()[b++]).ToArray();
}

Previous Next

isLucky

My solution:
bool isLucky(int n) 
{
    return Equals(n.ToString().Substring(0, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString())), n.ToString().Substring(n.ToString().Length / 2, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString())));
}

Previous Next

commonCharacterCount

My solution:
int commonCharacterCount(string s1, string s2) 
{
    return s1.ToCharArray().Distinct().Sum(c => Math.Min(s1.Count(x => x == c), s2.Count(x => x == c)));
}

Previous Next