Dada la string binaria S de tamaño N y un número K . La tarea es encontrar la substring más larga de 0 en la string que se forma repitiendo K veces la string dada.
Ejemplos:
Entrada: S = «100001», K = 3
Salida: 4
Después de repetir la string dada 3 veces, la string se convierte en 100001100001100001.
La substring más larga de 0 es 4
Entrada: S = «010001000», K = 4
Salida: 4
Acercarse:
- Si K es uno , encuentre la substring más larga de 0 en una string usando bucles simples
- Si K es mayor que uno , agregue una string dada al final de la string. Luego, la string se convierte en S + S y la longitud será 2 * N y encuentre la substring más larga de 0 en una string usando bucles simples
- Si la substring más larga es 2*N, nuestra respuesta será K*N
- De lo contrario, será nuestra respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the find the Longest continuous // sequence of '0' after repeating Given string K time #include <bits/stdc++.h> using namespace std; // Function to find the longest substring of 0's int longest_substring(string s, int k) { // To store size of the string int n = s.size(); if(k>1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (s[i] == '0' && i < n) x++, i++; ans = max(ans, x); i++; } // Check the conditions if(k==1 or ans!=n) return ans; else return (ans/2)*k; } // Driver code int main() { string s = "010001000"; int k = 4; // Function call cout << longest_substring(s, k); return 0; }
Java
// Java program to find the Longest continuous // sequence of '0' after repeating Given string K time class GFG { // Function to find the longest substring of 0's static int longest_substring(String s, int k) { // To store size of the string int n = s.length(); if(k > 1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (i < n && s.charAt(i) == '0') { x++; i++; } ans = Math.max(ans, x); i++; } // Check the conditions if(k == 1 || ans != n) return ans; else return (ans / 2) * k; } // Driver code public static void main(String[] args) { String s = "010001000"; int k = 4; // Function call System.out.println(longest_substring(s, k)); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to find the find the Longest continuous # sequence of '0' after repeating Given K time # Function to find the longest subof 0's def longest_substring(s, k): # To store size of the string n = len(s) if(k>1): s += s n *= 2 # To store the required answer ans = 0 # Find the longest subof 0's i = 0 while (i < n): x = 0 # Run a loop upto s[i] is zero while (i < n and s[i] == '0'): x,i=x+1, i+1 ans = max(ans, x) i+=1 # Check the conditions if(k==1 or ans!=n): return ans else: return (ans//2)*k # Driver code s = "010001000" k = 4 # Function call print(longest_substring(s, k)) # This code is contributed by mohit kumar 29
C#
// C# program to find the Longest continuous // sequence of '0' after repeating Given string K time using System; class GFG { // Function to find the longest substring of 0's static int longest_substring(String s, int k) { // To store size of the string int n = s.Length; if(k > 1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (i < n && s[i] == '0') { x++; i++; } ans = Math.Max(ans, x); i++; } // Check the conditions if(k == 1 || ans != n) return ans; else return (ans / 2) * k; } // Driver code public static void Main(String[] args) { String s = "010001000"; int k = 4; // Function call Console.WriteLine(longest_substring(s, k)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // Javascript program to find the find the Longest continuous // sequence of '0' after repeating Given string K time // Function to find the longest substring of 0's function longest_substring(s, k) { // To store size of the string var n = s.length; if(k>1) { s += s; n *= 2; } // To store the required answer var ans = 0; // Find the longest substring of 0's var i = 0; while (i < n) { var x = 0; // Run a loop upto s[i] is zero while (s[i] == '0' && i < n) x++, i++; ans = Math.max(ans, x); i++; } // Check the conditions if(k==1 || ans!=n) return ans; else return (ans/2)*k; } // Driver code var s = "010001000"; var k = 4; // Function call document.write( longest_substring(s, k)); </script>
Producción:
4