Check if K '0's can be flipped such that Binary String contains no pair of adjacent '1's
Last Updated :
14 Jul, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if k '0's can
// be flipped such that the string
// does not contain any pair of adjacent '1's
void canPlace(string s, int n, int k)
{
// Store the count of flips
int cnt = 0;
// Variable to iterate the string
int i = 0;
// Iterate over characters
// of the string
while (i < n) {
// If the current character
// is '1', increment i by 2
if (s[i] == '1') {
i += 2;
}
// Otherwise, 3 cases arises
else {
// If the current index
// is the starting index
if (i == 0) {
// If next character is '0'
if (s[i + 1] == '0') {
cnt++;
i += 2;
}
// Increment i by 1
else
i++;
}
// If the current index
// is the last index
else if (i == n - 1) {
// If previous character is '0'
if (s[i - 1] == '0') {
cnt++;
i += 2;
}
else
i++;
}
// For remaining characters
else {
// If both the adjacent
// characters are '0'
if (s[i + 1] == '0' && s[i - 1] == '0') {
cnt++;
i += 2;
}
else
i++;
}
}
}
// If cnt is at least K, print "Yes"
if (cnt >= k) {
cout << "Yes";
}
// Otherwise, print "No"
else {
cout << "No";
}
}
// Driver Code
int main()
{
string S = "10001";
int K = 1;
int N = S.size();
canPlace(S, N, K);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Function to check if k '0's can
// be flipped such that the string
// does not contain any pair of adjacent '1's
public static void canPlace(String s, int n, int k)
{
// Store the count of flips
int cnt = 0;
// Variable to iterate the string
int i = 0;
// Iterate over characters
// of the string
while (i < n)
{
// If the current character
// is '1', increment i by 2
if (s.charAt(i) == '1')
{
i += 2;
}
// Otherwise, 3 cases arises
else
{
// If the current index
// is the starting index
if (i == 0)
{
// If next character is '0'
if (s.charAt(i + 1) == '0')
{
cnt++;
i += 2;
}
// Increment i by 1
else
i++;
}
// If the current index
// is the last index
else if (i == n - 1)
{
// If previous character is '0'
if (s.charAt(i - 1) == '0')
{
cnt++;
i += 2;
}
else
i++;
}
// For remaining characters
else
{
// If both the adjacent
// characters are '0'
if (s.charAt(i + 1) == '0' && s.charAt(i - 1) == '0')
{
cnt++;
i += 2;
}
else
i++;
}
}
}
// If cnt is at least K, print "Yes"
if (cnt >= k)
{
System.out.println("Yes");
}
// Otherwise, print "No"
else
{
System.out.println("No");
}
}
// Driver code
public static void main (String[] args)
{
String S = "10001";
int K = 1;
int N = 5;
canPlace(S, N, K);
}
}
// This code is contributed by manupatharia.
Python3
# Python3 program for the above approach
# Function to check if k '0's can
# be flipped such that the string
# does not contain any pair of adjacent '1's
def canPlace(s, n, k) :
# Store the count of flips
cnt = 0
# Variable to iterate the string
i = 0
# Iterate over characters
# of the string
while (i < n) :
# If the current character
# is '1', increment i by 2
if (s[i] == '1') :
i += 2
# Otherwise, 3 cases arises
else :
# If the current index
# is the starting index
if (i == 0) :
# If next character is '0'
if (s[i + 1] == '0') :
cnt += 1
i += 2
# Increment i by 1
else :
i += 1
# If the current index
# is the last index
elif (i == n - 1) :
# If previous character is '0'
if (s[i - 1] == '0') :
cnt += 1
i += 2
else :
i += 1
# For remaining characters
else :
# If both the adjacent
# characters are '0'
if (s[i + 1] == '0' and s[i - 1] == '0') :
cnt += 1
i += 2
else :
i += 1
# If cnt is at least K, print "Yes"
if (cnt >= k) :
print("Yes")
# Otherwise, print "No"
else :
print("No")
# Driver code
S = "10001"
K = 1
N = len(S)
canPlace(S, N, K)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if k '0's can
// be flipped such that the string
// does not contain any pair of adjacent '1's
static void canPlace(string s, int n, int k)
{
// Store the count of flips
int cnt = 0;
// Variable to iterate the string
int i = 0;
// Iterate over characters
// of the string
while (i < n) {
// If the current character
// is '1', increment i by 2
if (s[i] == '1') {
i += 2;
}
// Otherwise, 3 cases arises
else {
// If the current index
// is the starting index
if (i == 0) {
// If next character is '0'
if (s[i + 1] == '0') {
cnt++;
i += 2;
}
// Increment i by 1
else
i++;
}
// If the current index
// is the last index
else if (i == n - 1) {
// If previous character is '0'
if (s[i - 1] == '0') {
cnt++;
i += 2;
}
else
i++;
}
// For remaining characters
else {
// If both the adjacent
// characters are '0'
if (s[i + 1] == '0' && s[i - 1] == '0') {
cnt++;
i += 2;
}
else
i++;
}
}
}
// If cnt is at least K, print "Yes"
if (cnt >= k)
{
Console.WriteLine("Yes");
}
// Otherwise, print "No"
else
{
Console.WriteLine("No");
}
}
// Driver Code
public static void Main(String[] args)
{
string S = "10001";
int K = 1;
int N = S.Length;
canPlace(S, N, K);
}
}
// This code is contributed by susmitakundugoaldanga.
JavaScript
<script>
/*package whatever //do not write package name here */
// Function to check if k '0's can
// be flipped such that the string
// does not contain any pair of adjacent '1's
function canPlace(s, n, k)
{
// Store the count of flips
var cnt = 0;
// Variable to iterate the string
var i = 0;
// Iterate over characters
// of the string
while (i < n)
{
// If the current character
// is '1', increment i by 2
if (s.charAt(i) == '1')
{
i += 2;
}
// Otherwise, 3 cases arises
else
{
// If the current index
// is the starting index
if (i == 0)
{
// If next character is '0'
if (s.charAt(i + 1) == '0')
{
cnt++;
i += 2;
}
// Increment i by 1
else
i++;
}
// If the current index
// is the last index
else if (i == n - 1)
{
// If previous character is '0'
if (s.charAt(i - 1) == '0')
{
cnt++;
i += 2;
}
else
i++;
}
// For remaining characters
else
{
// If both the adjacent
// characters are '0'
if (s.charAt(i + 1) == '0' && s.charAt(i - 1) == '0')
{
cnt++;
i += 2;
}
else
i++;
}
}
}
// If cnt is at least K, print "Yes"
if (cnt >= k)
{
document.write("Yes");
}
// Otherwise, print "No"
else
{
document.write("No");
}
}
// Driver code
var S = "10001";
var K = 1;
var N = 5;
canPlace(S, N, K);
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Check if K 0s can be flipped such that the given Array has no adjacent 1s Given a binary array arr[] of size N, and an integer K, the task is to check if K 0s can be flipped such that the array has no adjacent 1s. Examples: Input: arr[] = {0, 0, 0, 0, 1}, K=2Output: trueExplanation: The 0 at indices 0 and 2 can be replaced by 1. Hence 2 elements can be flipped (=K). Input
5 min read
Minimum flips required in a binary string such that all K-size substring contains 1 Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one '1'.Examples: Input: str = "0001", K = 2 Output: 1 Explanation: Flipping the bit at index 1 modifies str to "0101". All substrin
11 min read
Check if a Binary String can be split into disjoint subsequences which are equal to "010" Given a binary string, S of size N, the task is to check if it is possible to partition the string into disjoint subsequences equal to "010". Examples: Input: S = "010100"Output: YesExplanation: Partitioning the string in the manner 010100 to generate two subsequences equal to "010". Input: S = "010
8 min read
Check if count of 1s can be made greater in a Binary string by changing 0s adjacent to 1s Given a binary string S of size N, the task is to check if the count of 1s can be made greater than the count of 0s by changing the 0s adjacent to 1s to any other characters. If it is possible, then print Yes. Otherwise, print No. Note: Any index having 1 can be chosen at most once. Examples: Input:
9 min read
Check if a binary string contains all permutations of length k Given a binary string and k, to check whether it's contains all permutations of length k or not. Examples: Input : Binary string 11001 k : 2 Output : Yes 11001 contains all possibilities of binary sequences with k = 2, 00, 01, 10, 11 Input : Binary string: 1001 k : 2 Output: No 1001 does not contain
13 min read