Handle Score
@Ibrahim Nash 5725
@blackshadows 5685
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Complete Leaderboard
The Modified String
Easy Accuracy: 45.72% Submissions: 7215 Points: 2

Ishaan is playing with strings these days. He has found a new string. He wants to modify it as per the following rules to make it valid:

  • The string should not have three consecutive same characters (Refer example for explanation).
  • He can add any number of characters anywhere in the string. 

Find the minimum number of characters which Ishaan must insert in the string to make it valid.

Example 1:

S = aabbbcc
Output: 1
Explanation: In aabbbcc 3 b's occur
consecutively, we add a 'd',and Hence,
output will be aabbdbcc.

Example 1:

S = aaaaa
Output: 2
Explanation:  In aaaaa 5 a's occur
consecutively,we need to add 2 'b', and
Hence, the output will be aababaa.

Your Task:
This is a function problem. You only need to complete the function modified() that returns the answer.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints : 
1 <= Length of S <= 105


to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

The Modified String

Output Window