Update the answer with the number of elements between non - consecutive occurrences of the given letter
ans = ans + cnt ; cnt = 0 ; while ( i < str . length ( ) && str . charAt ( i ) != k ) { i = i + 1 ;