Short Palindrome Discussions | | HackerRank

Short Palindrome

  • + 0 comments

    C#

    public static int shortPalindrome(string s)
    {
        var (mod, occ1, occ2, occ3) = (1000000007, new int[26], new int[26, 26], new int[26, 26, 26]);
        return s.Select(c => c - 'a').Aggregate(0, (acc, c) =>
        {
            for (var i = 0; i < 26; i++)
            {
                acc = (acc + occ3[c, i, i]) % mod;
                occ3[i, c, c] = (occ3[i, c, c] + occ2[i, c]) % mod;
                occ2[i, c] = (occ2[i, c] + occ1[i]) % mod;
            }
            occ1[c]++;
            return acc;
        });
    }