DP is one approach. But it should take O(n^2)
opt(j) = maximum number of chars in C from a[0] to [j]
start(j) = largest i, st. a[i] to a[j] contains opt(j) chars in C
Then try to compute opt(j) from opt(j-1) and start(j-1).
You only need to start from start(j-1). If opt(j) is a new char, opt(j)=opt(
j-1)+1 (a hash or map needs to be maintained to store all chars in C that
have been seen), otherwise, update start(j) (e.g. if a[start(j-1)] = a[j],
etc.) This would take up to j. So at the end it ta