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 : 4
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:
- If K is one, then find the longest substring of 0’s in a string using a simple loops
- 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:
C++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static int longest_substring(String s, int k)
{
int n = s.length();
if (k > 1 )
{
s += s;
n *= 2 ;
}
int ans = 0 ;
int i = 0 ;
while (i < n)
{
int x = 0 ;
while (i < n && s.charAt(i) == '0' )
{
x++; i++;
}
ans = Math.max(ans, x);
i++;
}
if (k == 1 || ans != n)
return ans;
else
return (ans / 2 ) * k;
}
public static void main(String[] args)
{
String s = "010001000" ;
int k = 4 ;
System.out.println(longest_substring(s, k));
}
}
|
Python3
def longest_substring(s, k):
n = len (s)
if (k> 1 ):
s + = s
n * = 2
ans = 0
i = 0
while (i < n):
x = 0
while (i < n and s[i] = = '0' ):
x,i = x + 1 , i + 1
ans = max (ans, x)
i + = 1
if (k = = 1 or ans! = n):
return ans
else :
return (ans / / 2 ) * k
s = "010001000"
k = 4
print (longest_substring(s, k))
|
C#
using System;
class GFG
{
static int longest_substring(String s, int k)
{
int n = s.Length;
if (k > 1)
{
s += s;
n *= 2;
}
int ans = 0;
int i = 0;
while (i < n)
{
int x = 0;
while (i < n && s[i] == '0' )
{
x++; i++;
}
ans = Math.Max(ans, x);
i++;
}
if (k == 1 || ans != n)
return ans;
else
return (ans / 2) * k;
}
public static void Main(String[] args)
{
String s = "010001000" ;
int k = 4;
Console.WriteLine(longest_substring(s, k));
}
}
|
Javascript
<script>
function longest_substring(s, k)
{
var n = s.length;
if (k>1)
{
s += s;
n *= 2;
}
var ans = 0;
var i = 0;
while (i < n)
{
var x = 0;
while (s[i] == '0' && i < n)
x++, i++;
ans = Math.max(ans, x);
i++;
}
if (k==1 || ans!=n)
return ans;
else
return (ans/2)*k;
}
var s = "010001000" ;
var k = 4;
document.write( longest_substring(s, k));
</script>
|
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), for concatenating the string with itself.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...