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.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Longest Common Subsequence | DP-4
- Length of the longest substring without repeating characters
- Longest Non-palindromic substring
- Longest Palindromic Subsequence | DP-12
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.