Check if given Binary String can be made Palindrome using K flips
Last Updated :
24 Mar, 2022
Given a binary string str, the task is to determine if string str can be converted into a palindrome in K moves. In one move any one bit can be flipped i.e. 0 to 1 or 1 to 0.
Examples:
Input: str = “101100”, K = 1
Output: YES
Explanation: Flip last bit of str from 0 to 1.
Input: str = “0101101”, K = 2
Output: NO
Explanation: Three moves required to make str a palindrome.
Approach: The idea is to traverse the string with two pointers.
- Match the bits pointed by both pointers and
- Keep count of the failed comparisons.
- Number of unmatched pairs will be the desired output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinFlipPalindrome(string str, int K)
{
int n = str.size();
int i = 0, j = n - 1, count = 0;
while (i <= j) {
if (str[i] == str[j]) {
i += 1;
j -= 1;
continue ;
}
else {
i += 1;
j -= 1;
count += 1;
}
}
if (count <= K)
return 1;
else
return 0;
}
int main()
{
string str = "101011110" ;
int K = 2;
int res = MinFlipPalindrome(str, K);
if (res == 1)
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int MinFlipPalindrome( char * str, int K)
{
int n = strlen (str);
int i = 0, j = n - 1, count = 0;
while (i <= j) {
if (str[i] == str[j]) {
i += 1;
j -= 1;
continue ;
}
else {
i += 1;
j -= 1;
count += 1;
}
}
if (count <= K)
return 1;
else
return 0;
}
int main()
{
char * str = "101011110" ;
int K = 2;
int res = MinFlipPalindrome(str, K);
printf ( "%s\n" , (res ? "YES" : "NO" ));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MinFlipPalindrome(String str, int K)
{
int n = str.length();
int i = 0 , j = n - 1 , count = 0 ;
while (i <= j) {
if (str.charAt(i) == str.charAt(j)) {
i += 1 ;
j -= 1 ;
continue ;
}
else {
i += 1 ;
j -= 1 ;
count += 1 ;
}
}
if (count <= K)
return 1 ;
else
return 0 ;
}
public static void main(String[] args)
{
String str = "101011110" ;
int K = 2 ;
int res = MinFlipPalindrome(str, K);
if (res == 1 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def MinFlipPalindrome( str , K):
n = len ( str )
i = 0
j = n - 1
count = 0
while (i < = j):
if ( str [i] = = str [j]):
i + = 1
j - = 1
continue
else :
i + = 1
j - = 1
count + = 1
if (count < = K):
return 1
else :
return 0
str = "101011110"
K = 2
res = MinFlipPalindrome( str , K)
if (res = = 1 ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static int MinFlipPalindrome( string str, int K)
{
int n = str.Length;
int i = 0, j = n - 1, count = 0;
while (i <= j) {
if (str[i] == str[j]) {
i += 1;
j -= 1;
continue ;
}
else {
i += 1;
j -= 1;
count += 1;
}
}
if (count <= K)
return 1;
else
return 0;
}
public static void Main()
{
string str = "101011110" ;
int K = 2;
int res = MinFlipPalindrome(str, K);
if (res == 1)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
const MinFlipPalindrome = (str, K) => {
let n = str.length;
let i = 0, j = n - 1, count = 0;
while (i <= j) {
if (str[i] == str[j]) {
i += 1;
j -= 1;
continue ;
}
else {
i += 1;
j -= 1;
count += 1;
}
}
if (count <= K)
return 1;
else
return 0;
}
let str = "101011110" ;
let K = 2;
let res = MinFlipPalindrome(str, K);
if (res == 1)
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...