Dadas dos strings S y T de igual longitud. Ambas strings contienen solo los caracteres ‘0’ y ‘1’ . La tarea es encontrar el número mínimo de operaciones para convertir la string S en T. Hay 2 tipos de operaciones permitidas en la string S :
- Intercambiar dos caracteres cualquiera de la string.
- Reemplace un ‘0’ con un ‘1’ o viceversa.
Ejemplos:
Entrada: S = “011”, T = “101”
Salida: 1
Intercambia el primer y segundo carácter.Entrada: S = “010”, T = “101”
Salida: 2
Intercambie el primer y segundo carácter y reemplace el tercer carácter con ‘1’.
Enfoque: encuentre 2 valores para la string S , el número de índices que tienen 0 pero deberían ser 1 y el número de índices que tienen 1 pero deberían ser 0. El resultado sería el máximo de estos 2 valores ya que podemos usar intercambios en el mínimo de estos 2 valores y los caracteres no coincidentes restantes se pueden invertir, es decir , ‘0’ se puede cambiar a ‘1’ y ‘1’ se puede cambiar a ‘0’ .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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
Python3
# 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
<?php // PHP implementation of the approach // Function to return the minimum operations // of the given type required to convert // string s to string t function minOperations($s, $t, $n) { $ct0 = 0 ; $ct1 = 0; for ($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 $s = "010"; $t = "101"; $n = strlen($s); echo minOperations($s, $t, $n); // This code is contributed by Ryuga ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the minimum operations // of the given type required to convert // string s to string t function minOperations(s, t, n) { var ct0 = 0, ct1 = 0; for(var 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 var s = "010", t = "101"; var n = s.length; document.write(minOperations(s, t, n)); // This code is contributed by rdtank </script>
2
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA