Given a binary string S of length N and a positive integer M, the task is to check if it is possible to rotate the string circularly any number of times such that any pair of consecutive 1s are separated by at most M 0s. If it is possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “101001”, M = 1
Output: Yes
Explanation: Right shift the characters of the given string by 1 place. Therefore, the string S modifies to “110100”, leaving all pair of consecutive 1s separated by at most M(= 1) 0s.
Input: S = 1001001, M = 1
Output: No
Approach: The given problem can be solved based on the observation that if there are more than 1 pair of adjacent 1s having more than M number of 0s between them, then it is not possible to satisfy the given condition, because only such pair can be handled by rotating the string in such a way that all the 0s in between them are at the end.
Follow the steps below to solve the problem:
- Initialize a vector, say V, to store the indices of all 1s in the given string S.
- Initialize a variable, say, count, to store the number of pairs of 1s having more than M 0s between them.
- Traverse the given string S and store all indices of ‘1’ in the vector V.
- Traverse the vector V, starting from index 1 using a variable, say i, and perform the following steps:
- Store the number of 0s between indices V[i] and V[i – 1] in the string S in a variable T as (V[i] – V[i – 1] – 1).
- If the value of T is greater than M, then increment the value of count by 1.
- If the number of 0s between the first and last occurrence of ‘1’ is greater than M, then increment the value of count by 1.
- After completing the above steps, if the value of count is at most 1, then print Yes. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rotateString( int n, int m, string s)
{
vector< int > v;
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '1' ) {
v.push_back(i);
}
}
for ( int i = 1; i < ( int )v.size(); i++) {
if ((v[i] - v[i - 1] - 1) > m) {
cnt++;
}
}
if (v.size() >= 2
&& (n - (v.back() - v[0]) - 1) > m) {
cnt++;
}
if (cnt <= 1) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
string S = "101001" ;
int M = 1;
int N = S.size();
rotateString(N, M, S);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void rotateString( int n, int m, String s)
{
int v[] = new int [n];
int cnt = 0 ;
int j = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == '1' )
{
v[j] = i;
j += 1 ;
}
}
for ( int i = 1 ; i < j; i++)
{
if ((v[i] - v[i - 1 ] - 1 ) > m)
{
cnt++;
}
}
if (j >= 2 && (n - (v[j - 1 ] - v[ 0 ]) - 1 ) > m)
{
cnt++;
}
if (cnt <= 1 )
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
public static void main (String[] args)
{
String S = "101001" ;
int M = 1 ;
int N = S.length();
rotateString(N, M, S);
}
}
|
Python3
def rotateString(n, m, s):
v = []
cnt = 0
for i in range (n):
if (s[i] = = '1' ):
v.append(i)
for i in range ( 1 , len (v)):
if ((v[i] - v[i - 1 ] - 1 ) > m):
cnt + = 1
if ( len (v) > = 2 and
(n - (v[ - 1 ] - v[ 0 ]) - 1 ) > m):
cnt + = 1
if (cnt < = 1 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S = "101001"
M = 1
N = len (S)
rotateString(N, M, S)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void rotateString( int n, int m, string s)
{
List< int > v = new List< int >();
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '1' )
{
v.Add(i);
}
}
for ( int i = 1; i < v.Count; i++)
{
if ((v[i] - v[i - 1] - 1) > m)
{
cnt++;
}
}
if (v.Count >= 2 &&
(n - (v[v.Count - 1] - v[0]) - 1) > m)
{
cnt++;
}
if (cnt <= 1)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
public static void Main()
{
string S = "101001" ;
int M = 1;
int N = S.Length;
rotateString(N, M, S);
}
}
|
Javascript
<script>
function rotateString(n, m, s)
{
var v = [];
var cnt = 0;
var i;
for (i = 0; i < n; i++) {
if (s[i] == '1' ) {
v.push(i);
}
}
for (i = 1; i < v.length; i++) {
if ((v[i] - v[i - 1] - 1) > m) {
cnt++;
}
}
if (v.length >= 2
&& (n - (v[v.length-1] - v[0]) - 1) > m) {
cnt++;
}
if (cnt <= 1) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var S = "101001" ;
var M = 1;
var N = S.length;
rotateString(N, M, S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)