Replace all occurrences of string AB with C without using extra space

2.3

Given a string str that may contain one more occurrences of “AB”. Replace all occurrences of “AB” with “C” in str.

Examples:

Input  : str = "helloABworld"
Output : str = "helloCworld"

Input  : str = "fghABsdfABysu"
Output : str = "fghCsdfCysu"

A simple solution is to find all occurrences of “AB”. For every occurrence, replace it with C and more all characters one position back.

// C++ program to replace all occurrences of "AB"
// with "C"
#include <bits/stdc++.h>

void translate(char* str)
{
    if (str[0] == '\0')
        return;

    // Start traversing from second chracter
    for (int i=1; str[i] != '\0'; i++)
    {
        // If previous character is 'A' and
        // current character is 'B"
        if (str[i-1]=='A' && str[i]=='B')
        {
            // Replace previous character with
            // 'C' and move all subsequent
            // characters one position back
            str[i-1] = 'C';
            for (int j=i; str[j]!='\0'; j++)
                str[j] = str[j+1];
        }
    }
    return;
}

// Driver code
int main()
{
    char str[] = "helloABworldABGfG";
    translate(str);
    printf("The modified string is :\n");
    printf("%s", str);
}

Output:

The modified string is :
helloCworldCGfG

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

An efficient solution is to keep track of two indexes, one for modified string (i in below code) and other for original string (j in below code). If we find “AB” at current index j, we increment j by 2 and i by 1. Otherwise we increment both and copy character from j to i.

Below is implementation of above idea.

// Efficient C++ program to replace all occurrences
// of "AB" with "C"
#include <bits/stdc++.h>

void translate(char* str)
{
    int len = strlen(str);
    if (len < 2)
       return;

    int i = 0;  // Index in modified string
    int j = 0; // Index in original string

    // Traverse string
    while (j < len-1)
    {
        // Replace occurrence of "AB" with "C"
        if (str[j] == 'A' && str[j+1] == 'B')
        {
            // Increment j by 2
            j = j + 2;
            str[i++] = 'C';
            continue;
        }
        str[i++] = str[j++];
    }

    if (j == len-1)
      str[i++] = str[j];

    // add a null character to terminate string
    str[i] = '\0';
}

// Driver code
int main()
{
    char str[] = "helloABworldABGfG";
    translate(str);
    printf("The modified string is :\n");
    printf("%s", str);
}

Output:

The modified string is :
helloCworldCGfG

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

This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 16 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.