11 April 2017

Is MAC48 Address?

My solution:
bool isMAC48Address(string inputString) 
{
    return Regex.IsMatch(inputString, "^([0-9A-F]{2}-){5}[0-9A-F]{2}$");
}

Previous Next

Elections Winners

My solution:
int electionsWinners(int[] votes, int k) 
{
    var max = votes.Max();
    return k == 0 && votes.Where(d => d == votes.Max()).Count() == 1 ? 1 : votes.Count(t => t + k > max);
}

Previous Next

buildPalindrome

My solution:
string buildPalindrome(string st) 
{
    var i = 0;
    while (!st.Substring(i).ToArray().SequenceEqual(st.Substring(i).Reverse().ToArray()) && i < st.Length)
        i++;

    return st + new string(st.Substring(0, i).Reverse().ToArray());
}

Previous Next

Find Email Domain

My solution:
string findEmailDomain(string address) 
{
    return Regex.Match(address, "@[A-z0-9\\.-]+$").Value.Substring(1);
}

Previous Next

isBeautifulString

My solution:
bool isBeautifulString(string inputString) 
{
    var sorted = Enumerable.Range('a', inputString.Max(c => c) - 'a' + 1).Select(i => (char)i).ToArray();
    for (var i = 1; i < sorted.Length; i++)
        if (inputString.Count(c => c == sorted[i]) > inputString.Count(c => c == sorted[i - 1]))
            return false;

    return true;
}
Terms of use: http://stackoverflow.com/a/314508 by http://stackoverflow.com/users/4685/pop-catalin, modified under https://creativecommons.org/licenses/by-sa/3.0/.

Previous Next