Problem · String
Maximum Insertions Without Three Consecutive A
Learn this problemProblem statement
Write a function solution that, given a string S consisting of N characters, returns the maximum number of letters 'a' that can be inserted into S (including at the front and end of S) so that the resulting string doesn't contain three consecutive letters 'a'. If string S already contains the substring "aaa", then your function should return -1.
Function
solution(S: String) → intExamples
Example 1
S = "aabab"return = 3A string "aabaabaa" can be made.
Example 2
S = "dog"return = 8A string "aadaaoaagaa" can be made.
Example 3
S = "aa"return = 0No longer string can be made.
Example 4
S = "baaaa"return = -1There is a substring "aaa".
Constraints
Write an efficient algorithm for the following assumptions:
Nis an integer within the range[1..200,000].- String
Sis made only of lowercase letters('a' - 'z').