You are given a strng s of size n, consisting of characters A and B only. Ram wants to take out two disjoint segments out of its and he will count the number of As inside those segments, let us call this quantity x and sum of size of the two segments y. You have to find minimum y such that there exists two disjoint segments which have their x quantity >= z.
string s, n, x and z are given in the input.
Currently I have a solution of complexity $\mathbf{O}(n^2)$, Can anybody give a better solution than this?
Or can somebody prove that it is somehow related to 3 SUM problem?