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++
#include <bits/stdc++.h>
using namespace std;
string noAdjacentDup(string s)
{
int n = s.length();
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1])
{
s[i] = 'a' ;
while (s[i] == s[i - 1] ||
(i + 1 < n && s[i] == s[i + 1]))
s[i]++;
i++;
}
}
return s;
}
int main()
{
string s = "geeksforgeeks" ;
cout << noAdjacentDup(s);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static String noAdjacentDup(String s1)
{
int n = s1.length();
char [] s = s1.toCharArray();
for ( int i = 1 ; i < n; i++)
{
if (s[i] == s[i - 1 ])
{
s[i] = 'a' ;
while (s[i] == s[i - 1 ] ||
(i + 1 < n && s[i] == s[i + 1 ]))
s[i]++;
i++;
}
}
return ( new String(s));
}
public static void main(String argc[]){
String s = "geeksforgeeks" ;
System.out.println(noAdjacentDup(s));
}
}
|
Python3
def noAdjacentDup(s):
n = len (s)
for i in range ( 1 , n):
if (s[i] = = s[i - 1 ]):
s[i] = "a"
while (s[i] = = s[i - 1 ] or
(i + 1 < n and s[i] = = s[i + 1 ])):
s[i] + = 1
i + = 1
return s
s = list ( "geeksforgeeks" )
print ("".join(noAdjacentDup(s)))
|
C#
using System;
class GfG{
public static String noAdjacentDup(String s1)
{
int n = s1.Length;
char [] s = s1.ToCharArray();
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1])
{
s[i] = 'a' ;
while (s[i] == s[i - 1] ||
(i + 1 < n && s[i] == s[i + 1]))
s[i]++;
i++;
}
}
return ( new String(s));
}
public static void Main(String[] argc)
{
String s = "geeksforgeeks" ;
Console.Write(noAdjacentDup(s));
}
}
|
PHP
<?php
function noAdjacentDup( $s )
{
$n = strlen ( $s );
for ( $i = 1; $i < $n ; $i ++)
{
if ( $s [ $i ] == $s [ $i - 1])
{
$s [ $i ] = 'a' ;
while ( $s [ $i ] == $s [ $i - 1] ||
( $i + 1 < $n &&
$s [ $i ] == $s [ $i + 1]))
$s [ $i ]++;
$i ++;
}
}
return $s ;
}
$s = "geeksforgeeks" ;
echo (noAdjacentDup( $s ));
?>
|
Javascript
<script>
function noAdjacentDup(s1)
{
let n = s1.length;
let s = s1.split( '' );
for (let i = 1; i < n; i++)
{
if (s[i] == s[i - 1])
{
s[i] = 'a' ;
while (s[i] == s[i - 1] ||
(i + 1 < n && s[i] == s[i + 1]))
s[i]++;
i++;
}
}
return (s);
}
let s = "geeksforgeeks" ;
document.write(noAdjacentDup(s));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Jul, 2022
Like Article
Save Article