implement DP and use collections.Counter. The overall comp is O(n^2). Time limit exceeded