Given a string str of size N, the task is to find the minimum number of additions in the string such that no two consecutive elements are the same.
Explanation: Add an element between two r’s
Approach: The above problem can be solved using the above steps:
- Declare variable min_steps and initialise it by 0.
- Traverse the string using a for-loop from i=0 to i
- Check if the current character is the same as the character before it.
- If yes, then increment min_steps by 1.
- Else, continue the loop.
- Print min_steps as the answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)