# 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 any of the given strings can be replaced with any other character, print the count of 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 **i ^{th}** characters of all of 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 then two operations are required.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of operations required ` `const` `int` `minOperations(` `int` `n, string a, string b, string c) ` `{ ` ` ` ` ` `// To store the count of operations ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `char` `x = a[i]; ` ` ` `char` `y = b[i]; ` ` ` `char` `z = c[i]; ` ` ` ` ` `// No operation required ` ` ` `if` `(x == y && y == z) ` ` ` `; ` ` ` ` ` `// One operation is required when ` ` ` `// any two characters are equal ` ` ` `else` `if` `(x == y || y == z || x == z) ` ` ` `{ ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// Two operations are required when ` ` ` `// none of the characters are equal ` ` ` `else` ` ` `{ ` ` ` `ans += 2; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum count of operations required ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string a = ` `"place"` `; ` ` ` `string b = ` `"abcde"` `; ` ` ` `string c = ` `"plybe"` `; ` ` ` `int` `n = a.size(); ` ` ` `cout << minOperations(n, a, b, c); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the count of operations required ` ` ` `static` `int` `minOperations(` `int` `n, String a, String b, String c) ` ` ` `{ ` ` ` ` ` `// To store the count of operations ` ` ` `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); ` ` ` ` ` `// No operation required ` ` ` `if` `(x == y && y == z) ` ` ` `; ` ` ` ` ` `// One operation is required when ` ` ` `// any two characters are equal ` ` ` `else` `if` `(x == y || y == z || x == z) { ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// Two operations are required when ` ` ` `// none of the characters are equal ` ` ` `else` `{ ` ` ` `ans += ` `2` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum count of operations required ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the count ` `# of operations required ` `def` `minOperations(n, a, b, c): ` ` ` ` ` `# To store the count of operations ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `x ` `=` `a[i] ` ` ` `y ` `=` `b[i] ` ` ` `z ` `=` `c[i] ` ` ` ` ` `# No operation required ` ` ` `if` `(x ` `=` `=` `y ` `and` `y ` `=` `=` `z): ` ` ` `continue` ` ` ` ` `# One operation is required when ` ` ` `# any two characters are equal ` ` ` `elif` `(x ` `=` `=` `y ` `or` `y ` `=` `=` `z ` `or` `x ` `=` `=` `z): ` ` ` `ans ` `+` `=` `1` ` ` ` ` `# Two operations are required when ` ` ` `# none of the characters are equal ` ` ` `else` `: ` ` ` `ans ` `+` `=` `2` ` ` ` ` `# Return the minimum count ` ` ` `# of operations required ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `"place"` ` ` `b ` `=` `"abcde"` ` ` `c ` `=` `"plybe"` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(minOperations(n, a, b, c)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of operations required ` ` ` `static` `int` `minOperations(` `int` `n, ` `string` `a, ` `string` `b, ` `string` `c) ` ` ` `{ ` ` ` ` ` `// To store the count of operations ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `char` `x = a[i]; ` ` ` `char` `y = b[i]; ` ` ` `char` `z = c[i]; ` ` ` ` ` `// No operation required ` ` ` `if` `(x == y && y == z) ` ` ` `{;} ` ` ` ` ` `// One operation is required when ` ` ` `// any two characters are equal ` ` ` `else` `if` `(x == y || y == z || x == z) ` ` ` `{ ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// Two operations are required when ` ` ` `// none of the characters are equal ` ` ` `else` ` ` `{ ` ` ` `ans += 2; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum count of operations required ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `a = ` `"place"` `; ` ` ` `string` `b = ` `"abcde"` `; ` ` ` `string` `c = ` `"plybe"` `; ` ` ` `int` `n = a.Length; ` ` ` `Console.Write(minOperations(n, a, b, c)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count of ` `// operations required ` `function` `minOperations(` `$n` `, ` `$a` `, ` `$b` `, ` `$c` `) ` `{ ` ` ` ` ` `// To store the count of operations ` ` ` `$ans` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$x` `= ` `$a` `[` `$i` `]; ` ` ` `$y` `= ` `$b` `[` `$i` `]; ` ` ` `$z` `= ` `$c` `[` `$i` `]; ` ` ` ` ` `// No operation required ` ` ` `if` `(` `$x` `== ` `$y` `&& ` `$y` `== ` `$z` `) ` ` ` `; ` ` ` ` ` `// One operation is required when ` ` ` `// any two characters are equal ` ` ` `else` `if` `(` `$x` `== ` `$y` `|| ` ` ` `$y` `== ` `$z` `|| ` `$x` `== ` `$z` `) ` ` ` `{ ` ` ` `$ans` `++; ` ` ` `} ` ` ` ` ` `// Two operations are required when ` ` ` `// none of the characters are equal ` ` ` `else` ` ` `{ ` ` ` `$ans` `+= 2; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the minimum count of ` ` ` `// operations required ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$a` `= ` `"place"` `; ` `$b` `= ` `"abcde"` `; ` `$c` `= ` `"plybe"` `; ` `$n` `= ` `strlen` `(` `$a` `); ` `echo` `minOperations(` `$n` `, ` `$a` `, ` `$b` `, ` `$c` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum number of given operations required to make two strings equal
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Find the minimum number of preprocess moves required to make two strings equal
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Minimum move to end operations to make all strings equal
- Number of sub-strings that contain the given character exactly k times
- Minimum number of pairs required to make two strings same
- Append a digit in the end to make the number equal to the length of the remaining string
- Count of strings that become equal to one of the two strings after one removal
- Remove a character from a string to make it a palindrome
- Minimum operation require to make first and last character same
- Count of sub-strings that contain character X at least once
- Sub-strings that start and end with one character and have at least one other
- Sub-strings of length K containing same character

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.