letters made of the characters a, b and c, and a non-negative integer need. In one move you remove either the leftmost or the rightmost character of the string and keep it.
Return the smallest number of moves after which you hold at least need copies of each of the three letters. If that cannot happen, return -1.letters="cabbacca", need=1 → 3
Take c, a, b from the left: three letters, and we already hold one of each. Fewer is impossible, since we need at least three letters.
letters="abaacbc", need=2 → 6
Two letters from the left (a, b) and three from the right (c, b, c) give two bs and two cs but just one a, so one more from the left is needed: 6 in total.
letters="aabba", need=1 → -1
There is no c in the string at all, so the answer is -1.
letters contains only a, b and cletters="cabbacca", need=1
3
Run checks the first 3 tests in your browser. Submit checks all 128 tests on the server, hidden ones included.