VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-sub-string-of-0s-in-a-binary-string-which-is-repeated-k-times/

⇱ Longest sub string of 0's in a binary string which is repeated K times - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest sub string of 0's in a binary string which is repeated K times

Last Updated : 28 Oct, 2022

Given binary string S of size N and a number K. The task is to find the Longest sub string of 0's in the string which is formed by repeating given string K times. 
Examples: 

Input : S = "100001" , K = 3 
Output :
After repeating given string 3 time, string becomes 100001100001100001. 
The longest substring of 0's is 4 

Input : S = "010001000", K = 4 
Output : 4

 
 


Approach: 

  1. If K is one, then find the longest substring of 0's in a string using a simple loops
  2. If K is greater than one, then add a given string to the end of the string. Then string becomes S+S and length will be 2*N and find the longest substring of 0's in a string using a simple loops 
    • If the longest substring is 2*N then, our answer will be K*N
    • Otherwise it will be the our required answer


Below is the implementation of the above approach: 

Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), for concatenating the string with itself.

Comment
Article Tags: