Showing posts with label Exploring the Waters. Show all posts
Showing posts with label Exploring the Waters. Show all posts

01 April 2017

palindromeRearranging

My solution:
bool palindromeRearranging(string inputString)
{
    if (inputString.Length % 2 == 0 && inputString.Distinct().Any(c => inputString.Count(x => x == c) % 2 != 0))
        return false;
    if (inputString.Length % 2 == 1)
    {
        int count = 0;
        foreach (var c in inputString.Distinct())
        {
            if (inputString.Count(x => x == c) % 2 != 0)
                count++;
            if (count > 1)
                return false;
        }
    }

    return true;
}

Previous Next

arrayChange

My solution:
int arrayChange(int[] inputArray)
{
    int change = 0;
    for (int i = 0; i < inputArray.Length - 1; i++)
        while (inputArray[i] >= inputArray[i + 1])
        {
            inputArray[i + 1]++;
            change++;
        }

    return change;
}

Previous Next

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