Longest sub string of 0 's in a binary string which is repeated K times | C ++ 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 ; To store size of the string ; To store the required answer ; Find the longest substring of 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longest_substring ( string s , int k ) { int n = s . size ( ) ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( s [ i ] == '0' && i < n ) x ++ , i ++ ; ans = max ( ans , x ) ; i ++ ; } if ( k == 1 or ans != n ) return ans ; else return ( ans / 2 ) * k ; } int main ( ) { string s = "010001000" ; int k = 4 ; cout << longest_substring ( s , k ) ; return 0 ; }