Number of character corrections in the given strings to make them equal
Given three strings A, B, and C. Each of these is a string of length N consisting of lowercase English letters. The task is to make all the strings equal by performing an operation where any character of the given strings can be replaced with any other character, print the count of the minimum number of such operations required.
Examples:
Input: A = “place”, B = “abcde”, C = “plybe”
Output: 6
A = “place”, B = “abcde”, C = “plybe”.
We can achieve the task in the minimum number of operations by performing six operations as follows:
Change the first character in B to ‘p’. B is now “pbcde”
Change the second character in B to ‘l’. B is now “plcde”
Change the third character in B and C to ‘a’. B and C are now “plade” and “plabe” respectively.
Change the fourth character in B to ‘c’. B is now “place”
Change the fourth character in C to ‘c’. C is now “place”
Input: A = “game”, B = “game”, C = “game”
Output: 0
Approach: Run a loop, check if the ith characters of all the strings are equal then no operations are required. If two characters are equal then one operation is required and if all three characters are different from two operations are required.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int minOperations( int n, string a, string b, string c)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
char x = a[i];
char y = b[i];
char z = c[i];
if (x == y && y == z)
;
else if (x == y || y == z || x == z)
{
ans++;
}
else
{
ans += 2;
}
}
return ans;
}
int main()
{
string a = "place" ;
string b = "abcde" ;
string c = "plybe" ;
int n = a.size();
cout << minOperations(n, a, b, c);
return 0;
}
|
Java
class GFG {
static int minOperations( int n, String a, String b, String c)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
char x = a.charAt(i);
char y = b.charAt(i);
char z = c.charAt(i);
if (x == y && y == z)
;
else if (x == y || y == z || x == z) {
ans++;
}
else {
ans += 2 ;
}
}
return ans;
}
public static void main(String[] args)
{
String a = "place" ;
String b = "abcde" ;
String c = "plybe" ;
int n = a.length();
System.out.print(minOperations(n, a, b, c));
}
}
|
Python3
def minOperations(n, a, b, c):
ans = 0
for i in range (n):
x = a[i]
y = b[i]
z = c[i]
if (x = = y and y = = z):
continue
elif (x = = y or y = = z or x = = z):
ans + = 1
else :
ans + = 2
return ans
if __name__ = = '__main__' :
a = "place"
b = "abcde"
c = "plybe"
n = len (a)
print (minOperations(n, a, b, c))
|
C#
using System;
class GFG
{
static int minOperations( int n, string a, string b, string c)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
char x = a[i];
char y = b[i];
char z = c[i];
if (x == y && y == z)
{;}
else if (x == y || y == z || x == z)
{
ans++;
}
else
{
ans += 2;
}
}
return ans;
}
public static void Main()
{
string a = "place" ;
string b = "abcde" ;
string c = "plybe" ;
int n = a.Length;
Console.Write(minOperations(n, a, b, c));
}
}
|
PHP
<?php
function minOperations( $n , $a , $b , $c )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$x = $a [ $i ];
$y = $b [ $i ];
$z = $c [ $i ];
if ( $x == $y && $y == $z )
;
else if ( $x == $y ||
$y == $z || $x == $z )
{
$ans ++;
}
else
{
$ans += 2;
}
}
return $ans ;
}
$a = "place" ;
$b = "abcde" ;
$c = "plybe" ;
$n = strlen ( $a );
echo minOperations( $n , $a , $b , $c );
?>
|
Javascript
<script>
function minOperations(n, a, b, c)
{
let ans = 0;
for (let i = 0; i < n; i++)
{
let x = a[i];
let y = b[i];
let z = c[i];
if (x == y && y == z)
{;}
else if (x == y || y == z || x == z)
{
ans++;
}
else
{
ans += 2;
}
}
return ans;
}
let a = "place" ;
let b = "abcde" ;
let c = "plybe" ;
let n = a.length;
document.write(minOperations(n, a, b, c));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...