Given a binary string S of length N and an integer K, the task is to check if it is possible to flip K 0s such that the resulting string does not contain any pair of adjacent 1s. If it is possible to do so, then print “Yes”. Otherwise, print “No”.
Input: S = “01001001000”, K = 1
Output: Yes
Explanation:
Modifying S = “01001001000” to “01001001001” or modifying S = “01001001000″ to “01001001010″ satisfies the given condition.
Input: S = “000001000”, K = 4
Output: No
Approach: The idea to traverse the string and replace those ‘0′s with ‘1′s whose both adjacent characters are ‘0′ and flip one of the ‘0′s to count all the possible positions of flips such that it doesn’t affect the original string. Follow the steps below to solve the problem:
- Initialize s variable, say cnt as 0, to store the count of positions that can be flipped.
- Traverse the string using a variable i and perform the following steps:
- If the current character is ‘1′, then increment i by 2.
- Otherwise:
- If i = 0, and s[i + 1] = ‘0’: Increment cnt by 1 and i by 2. Otherwise, increment i by 1.
- If i = (N – 1), and s[i – 1] = ‘0’: Increment cnt by 1 and i by 2. Otherwise, increment i by 1.
- Otherwise, if s[i + 1] = ‘0’ and s[i – 1] = ‘0’: Increment cnt by 1 and i by 2. Otherwise, increment i by 1.
- After completing the above steps, if the value of cnt is at least K, then print “Yes”, Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void canPlace(string s, int n, int k)
{
int cnt = 0;
int i = 0;
while (i < n) {
if (s[i] == '1' ) {
i += 2;
}
else {
if (i == 0) {
if (s[i + 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
else if (i == n - 1) {
if (s[i - 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
else {
if (s[i + 1] == '0' && s[i - 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
}
}
if (cnt >= k) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
string S = "10001" ;
int K = 1;
int N = S.size();
canPlace(S, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void canPlace(String s, int n, int k)
{
int cnt = 0 ;
int i = 0 ;
while (i < n)
{
if (s.charAt(i) == '1' )
{
i += 2 ;
}
else
{
if (i == 0 )
{
if (s.charAt(i + 1 ) == '0' )
{
cnt++;
i += 2 ;
}
else
i++;
}
else if (i == n - 1 )
{
if (s.charAt(i - 1 ) == '0' )
{
cnt++;
i += 2 ;
}
else
i++;
}
else
{
if (s.charAt(i + 1 ) == '0' && s.charAt(i - 1 ) == '0' )
{
cnt++;
i += 2 ;
}
else
i++;
}
}
}
if (cnt >= k)
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main (String[] args)
{
String S = "10001" ;
int K = 1 ;
int N = 5 ;
canPlace(S, N, K);
}
}
|
Python3
def canPlace(s, n, k) :
cnt = 0
i = 0
while (i < n) :
if (s[i] = = '1' ) :
i + = 2
else :
if (i = = 0 ) :
if (s[i + 1 ] = = '0' ) :
cnt + = 1
i + = 2
else :
i + = 1
elif (i = = n - 1 ) :
if (s[i - 1 ] = = '0' ) :
cnt + = 1
i + = 2
else :
i + = 1
else :
if (s[i + 1 ] = = '0' and s[i - 1 ] = = '0' ) :
cnt + = 1
i + = 2
else :
i + = 1
if (cnt > = k) :
print ( "Yes" )
else :
print ( "No" )
S = "10001"
K = 1
N = len (S)
canPlace(S, N, K)
|
C#
using System;
class GFG
{
static void canPlace( string s, int n, int k)
{
int cnt = 0;
int i = 0;
while (i < n) {
if (s[i] == '1' ) {
i += 2;
}
else {
if (i == 0) {
if (s[i + 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
else if (i == n - 1) {
if (s[i - 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
else {
if (s[i + 1] == '0' && s[i - 1] == '0' ) {
cnt++;
i += 2;
}
else
i++;
}
}
}
if (cnt >= k)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String[] args)
{
string S = "10001" ;
int K = 1;
int N = S.Length;
canPlace(S, N, K);
}
}
|
Javascript
<script>
function canPlace(s, n, k)
{
var cnt = 0;
var i = 0;
while (i < n)
{
if (s.charAt(i) == '1' )
{
i += 2;
}
else
{
if (i == 0)
{
if (s.charAt(i + 1) == '0' )
{
cnt++;
i += 2;
}
else
i++;
}
else if (i == n - 1)
{
if (s.charAt(i - 1) == '0' )
{
cnt++;
i += 2;
}
else
i++;
}
else
{
if (s.charAt(i + 1) == '0' && s.charAt(i - 1) == '0' )
{
cnt++;
i += 2;
}
else
i++;
}
}
}
if (cnt >= k)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
}
var S = "10001" ;
var K = 1;
var N = 5;
canPlace(S, N, K);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)