Dada una string binaria str de longitud N , la tarea es encontrar el recuento máximo de substrings consecutivas en las que str se puede dividir de manera que todas las substrings estén balanceadas, es decir, tengan el mismo número de 0 y 1 . Si no es posible dividir str cumpliendo las condiciones, imprima -1 .
Ejemplo:
Entrada: str = “0100110101”
Salida: 4
Las substrings requeridas son “01”, “0011”, “01” y “01”.
Entrada: str = «0111100010»
Salida: 3Entrada: string = «0000000000»
Salida: -1
Enfoque: Inicialice el conteo = 0 y recorra la string carácter por carácter y realice un seguimiento del número de 0 y 1 hasta el momento, cada vez que el conteo de 0 y 1 sea igual, incremente el conteo. Como en la pregunta dada, si no es posible dividir la string, en ese momento, el conteo de 0 no debe ser igual al conteo de 1, luego devuelva -1 , de lo contrario, imprima el valor de conteo después del recorrido de la string completa.
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 count // of maximum substrings str // can be divided into int maxSubStr(string str, int n) { // To store the count of 0s and 1s int count0 = 0, count1 = 0; // To store the count of maximum // substrings str can be divided into int cnt = 0; for (int i = 0; i < n; i++) { if (str[i] == '0') { count0++; } else { count1++; } if (count0 == count1) { cnt++; } } // It is not possible to // split the string if (count0!=count1) { return -1; } return cnt; } // Driver code int main() { string str = "0100110101"; int n = str.length(); cout << maxSubStr(str, n); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to return the count // of maximum substrings str // can be divided into static int maxSubStr(String str, int n) { // To store the count of 0s and 1s int count0 = 0, count1 = 0; // To store the count of maximum // substrings str can be divided into int cnt = 0; for (int i = 0; i < n; i++) { if (str.charAt(i) == '0') { count0++; } else { count1++; } if (count0 == count1) { cnt++; } } // It is not possible to // split the string if (count0 != count1) { return -1; } return cnt; } // Driver code public static void main(String []args) { String str = "0100110101"; int n = str.length(); System.out.println(maxSubStr(str, n)); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach # Function to return the count # of maximum substrings str # can be divided into def maxSubStr(str, n): # To store the count of 0s and 1s count0 = 0 count1 = 0 # To store the count of maximum # substrings str can be divided into cnt = 0 for i in range(n): if str[i] == '0': count0 += 1 else: count1 += 1 if count0 == count1: cnt += 1 # It is not possible to # split the string if count0 != count1: return -1 return cnt # Driver code str = "0100110101" n = len(str) print(maxSubStr(str, n))
C#
// C# implementation of the above approach using System; class GFG { // Function to return the count // of maximum substrings str // can be divided into static int maxSubStr(String str, int n) { // To store the count of 0s and 1s int count0 = 0, count1 = 0; // To store the count of maximum // substrings str can be divided into int cnt = 0; for (int i = 0; i < n; i++) { if (str[i] == '0') { count0++; } else { count1++; } if (count0 == count1) { cnt++; } } // It is not possible to // split the string if (count0 != count1) { return -1; } return cnt; } // Driver code public static void Main(String []args) { String str = "0100110101"; int n = str.Length; Console.WriteLine(maxSubStr(str, n)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript implementation of the approach // Function to return the count // of maximum substrings str // can be divided into function maxSubStr(str, n) { // To store the count of 0s and 1s var count0 = 0, count1 = 0; // To store the count of maximum // substrings str can be divided into var cnt = 0; for (var i = 0; i < n; i++) { if (str[i] == '0') { count0++; } else { count1++; } if (count0 == count1) { cnt++; } } // It is not possible to // split the string if (count0 != count1) { return -1; } return cnt; } // Driver code var str = "0100110101"; var n = str.length; document.write( maxSubStr(str, n)); </script>
4
Complejidad temporal: O(N) donde N es la longitud de la string
Complejidad espacial: O(1)
Publicación traducida automáticamente
Artículo escrito por Prateek_Aggarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA