Given a binary string of length n and an integer k. Consider another string T which is formed by concatenating given binary string k times. The task is to print the maximum size of a substring of T containing only zeroes.
Input: str = 110010, k = 3
str = 110010 T = 110010110010110010(formed after 3
times concatenating str). So, the maximum size of a
substring of T(110010110010110010) containing only
zeroes is 2.
Input: str = 00100110, k = 5
Here, str = 00100110, T = 001001100010011000100110
0010011000100110. So, the maximum size of a substring
of T containing only zeroes is 3.
A Naive approach is to concatenate K copies of string and traverse through all the elements and count maximum size of substring containing only zeroes.
Efficient Approach :
There is no need to concatenate K copies.
- If string contains only zeroes then the answer is length_of_string * K.
- If string is comprised of both zeroes and ones, then the answer is either the maximum length of a substring of string containing only zeroes, or the sum of the length of the prefix of A containing only zeroes and the length of the suffix of string containing only zeroes.
- One thing to keep note of is that if K=1, then there is no need for prefix and suffix check.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find length of longest subsequence of one string which is substring of another string
- Longest substring of only 4's from the first N characters of the infinite string
- Longest palindromic string possible after removal of a substring
- Find the longest substring with k unique characters in a given string
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Longest palindromic String formed using concatenation of given strings in any order
- Find longest palindrome formed by removing or shuffling chars from string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Longest substring with count of 1s more than 0s
- Longest substring of vowels
- Longest Palindromic Substring | Set 2
- Longest Common Substring | DP-29
- Longest Non-palindromic substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.