Check if given Binary String can be made Palindrome using K flips Last Updated : 24 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 1Output: YESExplanation: Flip last bit of str from 0 to 1. Input: str = "0101101", K = 2Output: NOExplanation: 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 andKeep count of the failed comparisons.Number of unmatched pairs will be the desired output. Below is the implementation of the above approach: C++ // C++ program to check // if given binary string // can be made palindrome in K moves #include <bits/stdc++.h> using namespace std; // Function to make Binary string // palindrome in K steps 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; } // Driver code int main() { string str = "101011110"; int K = 2; int res = MinFlipPalindrome(str, K); if (res == 1) cout << "YES"; else cout << "NO"; return 0; } C // C program to check if given binary string // can be made palindrome in K moves #include <stdio.h> #include <string.h> // Function to make Binary string // palindrome in K steps 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; } // Driver code int main() { char* str = "101011110"; int K = 2; int res = MinFlipPalindrome(str, K); printf("%s\n", (res ? "YES" : "NO")); return 0; } // This code is contributed by phalasi. Java // Java program to check // if given binary string // can be made palindrome in K moves import java.io.*; class GFG { // Function to make Binary string // palindrome in K steps 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; } // Driver code 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"); } } // This code is contributed by Karandeep Singh Python3 # Python program for the above approach # Function to make Binary string # palindrome in K steps 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 # Driver code str = "101011110" K = 2 res = MinFlipPalindrome(str, K) if (res == 1): print("YES") else: print("NO") # This code is contributed by sanjoy_62. C# // C# program to check // if given binary string // can be made palindrome in K moves using System; class GFG { // Function to make Binary string // palindrome in K steps 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; } // Driver code 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"); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // JavaScript program to check // if given binary string // can be made palindrome in K moves // Function to make Binary string // palindrome in K steps 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; } // Driver code let str = "101011110"; let K = 2; let res = MinFlipPalindrome(str, K); if (res == 1) document.write("YES"); else document.write("NO"); // This code is contributed by rakeshsahni </script> OutputNO Time complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if Binary Array can be made Palindrome by flipping two bits at a time P pintusaini Follow Improve Article Tags : Strings Bit Magic Greedy DSA palindrome binary-string two-pointer-algorithm +3 More Practice Tags : Bit MagicGreedypalindromeStringstwo-pointer-algorithm +1 More Similar Reads Check if K palindromic strings can be formed from a given string Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously. Examples: Input: S = "annabelle", K = 2 Output: Yes Explanation: All characters of string S can be distributed into "elble" and "anna" w 7 min read Check if Binary Array can be made Palindrome by flipping two bits at a time Given a binary array A[] of size N, the task is to check whether the array can be converted into a palindrome by flipping two bits in each operation. Note: The operation can be performed any number of times. Examples: Input: A[] = {1, 0, 1, 0, 1, 1}Output: Yes?Explanation: We can perform the followi 7 min read Check if a given string is a rotation of a palindrome Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p 15+ min read Check if any anagram of a string is palindrome or not Given an anagram string S of length N, the task is to check whether it can be made palindrome or not. Examples: Input : S = geeksforgeeks Output: NoExplanation: There is no palindrome anagram of given string Input: S = geeksgeeksOutput: YesExplanation: There are palindrome anagrams of given string. 8 min read Check if given string can be made Palindrome by removing only single type of character Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times Examples: Input: S = "abczdzacb" Output: Yes Explanation: Remove first and second occurrence of character 'a', string S becomes "bczdzcb", which is a pal 7 min read Check if permutation of a given string can be made palindromic by removing at most K characters Given a string str and an integer K, the task is to check if a permutation of the given string can be made a palindromic by removing at most K characters from the given string. Examples: Input: str = "geeksforgeeks", K = 2 Output: Yes Explanation: Removing (str[5], str[6]) from the given string make 7 min read Like