Count of palindromes of length N having atmost k distinct characters such that no prefix (of size 2 to n-1) is palindrome.

for example

If N = 3 and K = 3, possible palindromes are "aba", "aca", "bab", "bcb", "cac" and "cbc". so count is 6.

Here is a link of problem and solution

https://www.hackerearth.com/problem/algorithm/avoid-prefix-palindromes-cdd47bd7-780d0bca/editorial/

Can anyone help me with this problem. Not able to understand the solution mentioned on hackerearth as there is no editorial there. Having difficult time understanding how states are defined in solution.