Dada una string binaria str , la tarea es eliminar la cantidad mínima de caracteres de la string binaria dada de modo que los caracteres en la string restante formen un orden ordenado.
Ejemplos:
Entrada: str = “1000101”
Salida: 2
Explicación:
La eliminación de las dos primeras apariciones de ‘1’ modifica la string a “00001”, que es un orden ordenado.
Por lo tanto, la cantidad mínima de caracteres que se eliminarán es 2.Entrada: str = “001111”
Salida: 0
Explicación:
La string ya está ordenada.
Por lo tanto, el recuento mínimo de caracteres que se eliminarán es 0.
Enfoque: la idea es contar el número de 1 s antes de la última aparición de 0 y el número de 0 después de la primera aparición de 1. El mínimo de los dos recuentos es el número necesario de caracteres que se eliminarán. A continuación se muestran los pasos:
- Recorre la string str y encuentra la posición de la primera aparición de 1 y la última aparición de 0 .
- Imprime 0 si la string tiene solo un tipo de carácter.
- Ahora, cuente el número de 1 que está presente antes de la última aparición de 0 y guárdelo en una variable, digamos cnt1 .
- Ahora, cuente el número de 0 s presentes después de la primera aparición de 1 en una variable, digamos cnt0 .
- Imprime el mínimo de cnt0 y cnt1 como el recuento mínimo de caracteres necesarios para eliminar.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum count // of characters to be removed to make // the string sorted in ascending order int minDeletion(string str) { // Length of given string int n = str.length(); // Stores the first // occurrence of '1' int firstIdx1 = -1; // Stores the last // occurrence of '0' int lastIdx0 = -1; // Traverse the string to find // the first occurrence of '1' for(int i = 0; i < n; i++) { if (str[i] == '1') { firstIdx1 = i; break; } } // Traverse the string to find // the last occurrence of '0' for(int i = n - 1; i >= 0; i--) { if (str[i] == '0') { lastIdx0 = i; break; } } // Return 0 if the str have // only one type of character if (firstIdx1 == -1 || lastIdx0 == -1) return 0; // Initialize count1 and count0 to // count '1's before lastIdx0 // and '0's after firstIdx1 int count1 = 0, count0 = 0; // Traverse the string to count0 for(int i = 0; i < lastIdx0; i++) { if (str[i] == '1') { count1++; } } // Traverse the string to count1 for(int i = firstIdx1 + 1; i < n; i++) { if (str[i] == '1') { count0++; } } // Return the minimum of // count0 and count1 return min(count0, count1); } // Driver code int main() { // Given string str string str = "1000101"; // Function call cout << minDeletion(str); return 0; } // This code is contributed by bikram2001jha
Java
// Java program for the above approach import java.util.*; import java.lang.*; class GFG { // Function to find the minimum count // of characters to be removed to make // the string sorted in ascending order static int minDeletion(String str) { // Length of given string int n = str.length(); // Stores the first // occurrence of '1' int firstIdx1 = -1; // Stores the last // occurrence of '0' int lastIdx0 = -1; // Traverse the string to find // the first occurrence of '1' for (int i = 0; i < n; i++) { if (str.charAt(i) == '1') { firstIdx1 = i; break; } } // Traverse the string to find // the last occurrence of '0' for (int i = n - 1; i >= 0; i--) { if (str.charAt(i) == '0') { lastIdx0 = i; break; } } // Return 0 if the str have // only one type of character if (firstIdx1 == -1 || lastIdx0 == -1) return 0; // Initialize count1 and count0 to // count '1's before lastIdx0 // and '0's after firstIdx1 int count1 = 0, count0 = 0; // Traverse the string to count0 for (int i = 0; i < lastIdx0; i++) { if (str.charAt(i) == '1') { count1++; } } // Traverse the string to count1 for (int i = firstIdx1 + 1; i < n; i++) { if (str.charAt(i) == '1') { count0++; } } // Return the minimum of // count0 and count1 return Math.min(count0, count1); } // Driver Code public static void main(String[] args) { // Given string str String str = "1000101"; // Function Call System.out.println(minDeletion(str)); } }
Python3
# Python3 program for the above approach # Function to find the minimum count # of characters to be removed to make # the string sorted in ascending order def minDeletion(s): # Length of given string n = len(s) # Stores the first # occurrence of '1' firstIdx1 = -1 # Stores the last # occurrence of '0' lastIdx0 = -1 # Traverse the string to find # the first occurrence of '1' for i in range(0, n): if (str[i] == '1'): firstIdx1 = i break # Traverse the string to find # the last occurrence of '0' for i in range(n - 1, -1, -1): if (str[i] == '0'): lastIdx0 = i break # Return 0 if the str have # only one type of character if (firstIdx1 == -1 or lastIdx0 == -1): return 0 # Initialize count1 and count0 to # count '1's before lastIdx0 # and '0's after firstIdx1 count1 = 0 count0 = 0 # Traverse the string to count0 for i in range(0, lastIdx0): if (str[i] == '1'): count1 += 1 # Traverse the string to count1 for i in range(firstIdx1 + 1, n): if (str[i] == '1'): count0 += 1 # Return the minimum of # count0 and count1 return min(count0, count1) # Driver code # Given string str str = "1000101" # Function call print(minDeletion(str)) # This code is contributed by Stream_Cipher
C#
// C# program for the above approach using System.Collections.Generic; using System; class GFG{ // Function to find the minimum count // of characters to be removed to make // the string sorted in ascending order static int minDeletion(string str) { // Length of given string int n = str.Length; // Stores the first // occurrence of '1' int firstIdx1 = -1; // Stores the last // occurrence of '0' int lastIdx0 = -1; // Traverse the string to find // the first occurrence of '1' for(int i = 0; i < n; i++) { if (str[i] == '1') { firstIdx1 = i; break; } } // Traverse the string to find // the last occurrence of '0' for(int i = n - 1; i >= 0; i--) { if (str[i] == '0') { lastIdx0 = i; break; } } // Return 0 if the str have // only one type of character if (firstIdx1 == -1 || lastIdx0 == -1) return 0; // Initialize count1 and count0 to // count '1's before lastIdx0 // and '0's after firstIdx1 int count1 = 0, count0 = 0; // Traverse the string to count0 for(int i = 0; i < lastIdx0; i++) { if (str[i] == '1') { count1++; } } // Traverse the string to count1 for(int i = firstIdx1 + 1; i < n; i++) { if (str[i] == '1') { count0++; } } // Return the minimum of // count0 and count1 return Math.Min(count0, count1); } // Driver Code public static void Main() { // Given string str string str = "1000101"; // Function call Console.WriteLine(minDeletion(str)); } } // This code is contributed by Stream_Cipher
Javascript
<script> // JavaScript program to implement // the above approach // Function to find the minimum count // of characters to be removed to make // the string sorted in ascending order function minDeletion(str) { // Length of given string let n = str.length; // Stores the first // occurrence of '1' let firstIdx1 = -1; // Stores the last // occurrence of '0' let lastIdx0 = -1; // Traverse the string to find // the first occurrence of '1' for(let i = 0; i < n; i++) { if (str[i] == '1') { firstIdx1 = i; break; } } // Traverse the string to find // the last occurrence of '0' for(let i = n - 1; i >= 0; i--) { if (str[i] == '0') { lastIdx0 = i; break; } } // Return 0 if the str have // only one type of character if (firstIdx1 == -1 || lastIdx0 == -1) return 0; // Initialize count1 and count0 to // count '1's before lastIdx0 // and '0's after firstIdx1 let count1 = 0, count0 = 0; // Traverse the string to count0 for(let i = 0; i < lastIdx0; i++) { if (str[i] == '1') { count1++; } } // Traverse the string to count1 for(let i = firstIdx1 + 1; i < n; i++) { if (str[i] == '1') { count0++; } } // Return the minimum of // count0 and count1 return Math.min(count0, count1); } // Driver code // Given string str let str = "1000101"; // Function call document.write(minDeletion(str)); // This code is contributed by target_2. </script>
2
Complejidad temporal: O(N)
Espacio auxiliar: O(1)