Print a closest string that does not contain adjacent duplicates

1.5

Given a string S, change the smallest number of letters in S such that all adjacent characters are different. Print the resultant string.

Examples :

Input : S = "aab"
Output: acb
Explanation :
Loop will start for i-th character, which 
is second ‘a’. It’s cannot be ‘b’ since it 
matches with third char. So output should
be ‘acb’.

Input : S = "geeksforgeeks"
Output: geaksforgeaks
Explanation :
Resultant string, after making minimal 
changes. S = "geaksforgeaks". We made two 
changes, which is the optimal solution here.

We can solve this problem using greedy approach. Let us consider a segment of length k of consecutive identical characters. We have to make at least [K/2] changes in the segment, to make that there are no identical characters in a row. We can also change the second, fourth etc.. characters of the string that is it should not be equal to the letter on the left side and the letter to the right side.

Traverse the string from starting index (i = 1) and if any two adjacent letters( i & i-1) are equal then initialize (i)th character with ‘a’ and start another loop to make (i)th character different from the left and right letters.

Below is the implementation of above approach :

C++

// C++ program to print a string with no adjacent
// duplicates by doing  minimum changes to original
// string
#include <bits/stdc++.h>
using namespace std;

// Function to print simple string
string noAdjacentDup(string s)
{
    int n = s.length();
    for (int i = 1; i < n; i++) 
    {
        // If any two adjacent characters are equal
        if (s[i] == s[i - 1]) 
        {
            s[i] = 'a'; // Initialize it to 'a'

            // Traverse the loop until it is different
            // from the left and right letter.
            while (s[i] == s[i - 1] || 
                (i + 1 < n && s[i] == s[i + 1]))             
                s[i]++;
            
            i++; 
        }
    }
    return s;
}

// Driver Function
int main()
{
    string s = "geeksforgeeks";
    cout << noAdjacentDup(s);
    return 0;
}

Java

// Java program to print a string with
// no adjacent duplicates by doing 
// minimum changes to original string
import java.util.*;
import java.lang.*;

public class GfG{
    
    // Function to print simple string
    public static String noAdjacentDup(String s1)
    {
        int n = s1.length();
        char[] s = s1.toCharArray();
        for (int i = 1; i < n; i++) 
        {
            // If any two adjacent 
            // characters are equal
            if (s[i] == s[i - 1]) 
            {
                // Initialize it to 'a'
                s[i] = 'a'; 
                
                // Traverse the loop until it  
                // is different from the left
                // and right letter.
                while (s[i] == s[i - 1] || 
                    (i + 1 < n && s[i] == s[i + 1]))             
                    s[i]++;
            
                i++; 
            }
        }
        return (new String(s));
    }
    
    // Driver function
    public static void main(String argc[]){

        String s = "geeksforgeeks";
        System.out.println(noAdjacentDup(s));
        
    }
    
}

/* This code is contributed by Sagar Shukla */

Output :

geaksforgeaks

Check out this Author's contributed articles.

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:



1.5 Average Difficulty : 1.5/5.0
Based on 2 vote(s)