Minimum replacements to make adjacent characters unequal in a ternary string
Given a string of ‘0’, ‘1’ and ‘2’. The task is to find the minimum number of replacements such that the adjacent characters are not equal.
Examples:
Input: s = “201220211”
Output: 2
Resultant string after changes is 201210210
Input: s = “0120102”
Output: 0
Approach: The following problem can be solved using the greedy method. We can greedily compare every adjacent pair. If the adjacent pairs which are characters at ith and i-1th are the same, then replace the ith character with a character that is not equal to the character at i-1th and i+1th index. In the case of the last adjacent pair, just replace it with the character which is not equal to the character at the i-1th index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinimalReplacements(string s)
{
int n = s.length();
int cnt = 0;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1]) {
cnt += 1;
if (i != (n - 1)) {
for ( auto it : "012" ) {
if (it != s[i + 1] &&
it != s[i - 1]) {
s[i] = it;
break ;
}
}
}
else
{
for ( auto it : "012" ) {
if (it != s[i - 1]) {
s[i] = it;
break ;
}
}
}
}
}
return cnt;
}
int main()
{
string s = "201220211" ;
cout << countMinimalReplacements(s);
return 0;
}
|
Java
class GFG
{
static final int MAX = 26 ;
static int countMinimalReplacements( char [] s)
{
int n = s.length;
int cnt = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (s[i] == s[i - 1 ])
{
cnt += 1 ;
if (i != (n - 1 ))
{
for ( char it : "012" .toCharArray())
{
if (it != s[i + 1 ]
&& it != s[i - 1 ])
{
s[i] = it;
break ;
}
}
}
else
{
for ( char it : "012" .toCharArray())
{
if (it != s[i - 1 ])
{
s[i] = it;
break ;
}
}
}
}
}
return cnt;
}
public static void main(String[] args)
{
String s = "201220211" ;
System.out.println(countMinimalReplacements(s.toCharArray()));
}
}
|
Python3
def countMinimalReplacements(s):
n = len (s)
cnt = 0
for i in range ( 1 , n):
if (s[i] = = s[i - 1 ]):
cnt + = 1 ;
if (i ! = (n - 1 )):
s = list (s)
for j in "012" :
if (j ! = s[i + 1 ] and
j ! = s[i - 1 ]):
s[i] = j
break
s = ''.join(s)
else :
s = list (s)
for k in "012" :
if (k ! = s[i - 1 ]):
s[i] = k
break
s = ''.join(s)
return cnt
if __name__ = = '__main__' :
s = "201220211"
print (countMinimalReplacements(s))
|
C#
using System;
class GFG
{
static readonly int MAX = 26;
static int countMinimalReplacements( char [] s)
{
int n = s.Length;
int cnt = 0;
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1])
{
cnt += 1;
if (i != (n - 1))
{
foreach ( char it in "012" .ToCharArray())
{
if (it != s[i + 1] &&
it != s[i - 1])
{
s[i] = it;
break ;
}
}
}
else
{
foreach ( char it in "012" .ToCharArray())
{
if (it != s[i - 1])
{
s[i] = it;
break ;
}
}
}
}
}
return cnt;
}
public static void Main(String[] args)
{
String s = "201220211" ;
Console.WriteLine(countMinimalReplacements(s.ToCharArray()));
}
}
|
PHP
<?php
function countMinimalReplacements( $s )
{
$n = strlen ( $s );
$cnt = 0;
$str = "012" ;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $s [ $i ] == $s [ $i - 1])
{
$cnt += 1;
if ( $i != ( $n - 1))
{
for ( $it = 0 ; $it < strlen ( $str ); $it ++)
{
if ( $str [ $it ] != $s [ $i + 1] &&
$str [ $it ] != $s [ $i - 1])
{
$s [ $i ] = $str [ $it ];
break ;
}
}
}
else
{
for ( $it = 0 ; $it < strlen ( $str ); $it ++)
{
if ( $str [ $it ] != $s [ $i - 1])
{
$s [ $i ] = $str [ $it ];
break ;
}
}
}
}
}
return $cnt ;
}
$s = "201220211" ;
echo countMinimalReplacements( $s );
?>
|
Javascript
<script>
function countMinimalReplacements(s) {
var n = s.length;
var cnt = 0;
var str = "012" ;
for ( var i = 1; i < n; i++) {
if (s[i] === s[i - 1]) {
cnt += 1;
if (i !== n - 1) {
for ( var it = 0; it < str.length; it++) {
if (str[it] !== s[i + 1] && str[it] !== s[i - 1]) {
s[i] = str[it];
break ;
}
}
}
else {
for ( var it = 0; it < str.length; it++) {
if (str[it] !== s[i - 1]) {
s[i] = str[it];
break ;
}
}
}
}
}
return cnt;
}
var s = "201220211" ;
document.write(countMinimalReplacements(s));
</script>
|
Complexity Analysis:
- Time Complexity: O(3*n), as we are using a loop to traverse n times. Where n is the number of elements in the array.
- Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...