Quick Telecast
Expect News First

Minimum number of insertions in given String to remove adjacent duplicates

0 71


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.

Examples:

Input: str=”rrg” 
Output: 1
Explanation: Add an element between two r’s

Input: str=”rrrrr”
Output: 4

 

Approach: The above problem can be solved using the above steps:

  1. Declare variable min_steps and initialise it by 0.
  2. Traverse the string using a for-loop from i=0 to i<N.
  3. Check if the current character is the same as the character before it.
  4. If yes, then increment min_steps by 1.
  5. Else, continue the loop.
  6. Print min_steps as the answer.

Below is the implementation of the above approach:

C++

 

#include <iostream>

using namespace std;

 

int minAdditions(string str)

{

    

    int len = str.size();

 

    

    

    int min_steps = 0;

 

    int i;

 

    

    

    for (i = 1; i < len; i++) {

        if (str[i] == str[i - 1])

            min_steps++;

    }

 

    

    return min_steps;

}

 

int main()

{

    string str = "RRG";

    cout << minAdditions(str);

    return 0;

}

Python3

 

def minAdditions(str):

 

    

    le = len(str)

 

    

    

    min_steps = 0

 

    

    

    for i in range(1, le):

        if (str[i] == str[i - 1]):

            min_steps += 1

 

    

    return min_steps

 

 

if __name__ == "__main__":

 

    str = "RRG"

    print(minAdditions(str))

 

 
 

 

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

 


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.

Examples:

Input: str=”rrg” 
Output: 1
Explanation: Add an element between two r’s

Input: str=”rrrrr”
Output: 4

 

Approach: The above problem can be solved using the above steps:

  1. Declare variable min_steps and initialise it by 0.
  2. Traverse the string using a for-loop from i=0 to i<N.
  3. Check if the current character is the same as the character before it.
  4. If yes, then increment min_steps by 1.
  5. Else, continue the loop.
  6. Print min_steps as the answer.

Below is the implementation of the above approach:

C++

 

#include <iostream>

using namespace std;

 

int minAdditions(string str)

{

    

    int len = str.size();

 

    

    

    int min_steps = 0;

 

    int i;

 

    

    

    for (i = 1; i < len; i++) {

        if (str[i] == str[i - 1])

            min_steps++;

    }

 

    

    return min_steps;

}

 

int main()

{

    string str = "RRG";

    cout << minAdditions(str);

    return 0;

}

Python3

 

def minAdditions(str):

 

    

    le = len(str)

 

    

    

    min_steps = 0

 

    

    

    for i in range(1, le):

        if (str[i] == str[i - 1]):

            min_steps += 1

 

    

    return min_steps

 

 

if __name__ == "__main__":

 

    str = "RRG"

    print(minAdditions(str))

 

 
 

 

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

 

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Quick Telecast is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment
buy kamagra buy kamagra online
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock