Gotham City is the home of Batman, and Batman likes to keep his people together.
There are N houses in the city. The resident of the i^{th} house is of type A_i. It is known that people of the same type are friends and people of different types are not friends.
To maintain peace, Batman can do the following type of operation:
Choose indices i and j(1\leq i \le j \le N) and reverse the substring A[i,j].
Determine the length of the longest range of friends after applying at mostk operations on the string for all 0 \le k \le N.
EXPLANATION:
First let’s see how we can increase the length of substrings having same character by the reversal operation as given in the question.
For a string say, s and for character say, x, we first take the longest and 2nd longest substrings having all characters as x. We are taking the longest and 2nd longest since we want to maximize the length of the substring formed in the given operation. Say they are at position i_1 and i_2 with length l_1 and l_2. Assuming i_1 is less than i_2, we can just reverse the substring s[i_1....i_2 - 1] to get substring of length (l_1+l_2) having all x. We can keep repeating this operation till we get the longest substring that contains all the x that are present in string s. Also we would observe that the number of operations required to get the above longest substring would be the number of substrings of x in the original string which would be always less than n, i.e the length of the string s.
Now coming back to this question, what we can do is repeat the operations as defined in the above paragraph for each character from a to z and for each operation take the length of the maximum possible substring that can be formed among a to z as our answer for that operation.
According to my experience, I have seen multiple cases when my map (ordered/unordered) implementation gave TLE but vector implementation gave AC.
I would suggest that whenever you know what keys the map is going to have and its contiguous => use a vector, as it ensures O(1)
It should not be giving TLE according to the constraints, IDK why it is, but one suggestion for next time: Please don’t keep bottleneck constraints like these…as you can see the basic motive to solve the problem was achieved, was even able to implement it but it TLEd for an unknown reason and I lost my rating.