You are given an integer $$$N$$$, and a string $$$S$$$ of length $$$N$$$ that only contains W
and L
. How many substrings $$$[l,r]$$$ in $$$S$$$ that satisfies:
$$$S_l=S_r=\texttt{W}$$$
The value of the count of
W
in $$$S_{[l,r]}$$$ minus the count ofL
in $$$S_{[l,r]}$$$ is greater than or equal to $$$K$$$ ($$$K$$$ is an integer and $$$K>0$$$).
I think this can be solved in O(N) only using two pointers.
you can do prefix sums , then fix the l , count the r with the prefix sum > prefix sum of l + k
this can be done with segment tree or ordered set in nlogn
maintain two fenwick trees / segment trees/ ordered sets, holding prefix sums ending at W or L
I guess you can do sliding window and solve it in O(N)
not nlog but do a prefix sum where W: 1 and L: -1 then you just 2pointer it