📌  相关文章
📜  检查给定的二进制字符串是否可以使用 K 次翻转制成回文

📅  最后修改于: 2022-05-13 01:56:04.851000             🧑  作者: Mango

检查给定的二进制字符串是否可以使用 K 次翻转制成回文

给定一个二进制字符串str ,任务是确定字符串str是否可以在K步中转换为回文。在一次移动中,任何一位都可以翻转,即0110

例子

方法:想法是用两个指针遍历字符串。

  • 匹配两个指针指向的位和
  • 记录失败的比较。
  • 不匹配对的数量将是所需的输出。

下面是上述方法的实现:

C++
// C++ program to check
// if given binary string
// can be made palindrome in K moves
 
#include 
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 
#include 
 
// 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



输出
NO

时间复杂度:O(N)
辅助空间:O(1)