# Minimum number of given operations required to convert a string to another string

Given two strings S and T of equal length. Both strings contain only the characters ‘0’ and ‘1’. The task is to find the minimum number of operations to convert string S to T. There are 2 types of operations allowed on string S:

• Swap any two characters of the string.
• Replace a ‘0’ with a ‘1’ or vice versa.

Examples:

Input: S = “011”, T = “101”
Output: 1
Swap the first and second character.

Input: S = “010”, T = “101”
Output: 2
Swap the first and second character and replace the third character with ‘1’.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Find 2 values for the string S, the number of indices that have 0 but should be 1 and the number of indices that have 1 but should be 0. The result would be the maximum of these 2 values since we can use swaps on the minimum of these 2 values and the remaining unmatched characters can be inverted i.e. ‘0’ can be changed to ‘1’ and ‘1’ can be changed to ‘0’.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum operations ` `// of the given type required to convert ` `// string s to string t ` `int` `minOperations(string s, string t, ``int` `n) ` `{ ` `    ``int` `ct0 = 0, ct1 = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Characters are already equal ` `        ``if` `(s[i] == t[i]) ` `            ``continue``; ` ` `  `        ``// Increment count of 0s ` `        ``if` `(s[i] == ``'0'``) ` `            ``ct0++; ` ` `  `        ``// Increment count of 1s ` `        ``else` `            ``ct1++; ` `    ``} ` ` `  `    ``return` `max(ct0, ct1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"010"``, t = ``"101"``; ` `    ``int` `n = s.length(); ` `    ``cout << minOperations(s, t, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the minimum  ` `// operations of the given type required  ` `// to convert string s to string t ` `static` `int` `minOperations(String s,  ` `                        ``String t, ``int` `n) ` `{ ` `    ``int` `ct0 = ``0``, ct1 = ``0``; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// Characters are already equal ` `        ``if` `(s.charAt(i) == t.charAt(i)) ` `            ``continue``; ` ` `  `        ``// Increment count of 0s ` `        ``if` `(s.charAt(i) == ``'0'``) ` `            ``ct0++; ` ` `  `        ``// Increment count of 1s ` `        ``else` `            ``ct1++; ` `    ``} ` ` `  `    ``return` `Math.max(ct0, ct1); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``String s = ``"010"``, t = ``"101"``; ` `    ``int` `n = s.length(); ` `    ``System.out.println(minOperations(s, t, n)); ` `} ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Python 3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the minimum operations ` `# of the given type required to convert ` `# string s to string t ` `def` `minOperations(s, t, n): ` ` `  `    ``ct0 ``=` `0` `    ``ct1 ``=` `0` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# Characters are already equal ` `        ``if` `(s[i] ``=``=` `t[i]): ` `            ``continue` ` `  `        ``# Increment count of 0s ` `        ``if` `(s[i] ``=``=` `'0'``): ` `            ``ct0 ``+``=` `1` ` `  `        ``# Increment count of 1s ` `        ``else``: ` `            ``ct1 ``+``=` `1` ` `  `    ``return` `max``(ct0, ct1) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``s ``=` `"010"` `    ``t ``=` `"101"` `    ``n ``=` `len``(s) ` `    ``print``(minOperations(s, t, n)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` `  `// Function to return the minimum operations ` `// of the given type required to convert ` `// string s to string t ` `static` `int` `minOperations(``string` `s,   ` `                         ``string` `t, ``int` `n) ` `{ ` `    ``int` `ct0 = 0, ct1 = 0; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``// Characters are already equal ` `        ``if` `(s[i] == t[i]) ` `            ``continue``; ` ` `  `        ``// Increment count of 0s ` `        ``if` `(s[i] == ``'0'``) ` `            ``ct0++; ` ` `  `        ``// Increment count of 1s ` `        ``else` `            ``ct1++; ` `    ``} ` ` `  `    ``return` `Math.Max(ct0, ct1); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `s = ``"010"``, t = ``"101"``; ` `    ``int` `n = s.Length; ` `    ``Console.Write(minOperations(s, t, n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 ` `

Output:

```2
```

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

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

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.