📜  通过将二进制字符串反转和补充 K 次获得的字符串

📅  最后修改于: 2021-09-07 02:06:39             🧑  作者: Mango

给定一个大小为N的二进制字符串和一个整数K ,任务是对字符串执行K 次操作并打印最终字符串:

  • 如果操作数为奇数,则反转字符串,
  • 如果操作数为偶数,则对字符串进行补码。

例子:

天真的方法:
遍历所有 K 个步骤,如果当前步骤为奇数,则执行反向操作,否则对字符串补码。
有效的方法:在观察给定的操作模式后:

  • 如果将字符串反转偶数次,则获得原始字符串。
  • 类似地,如果一个字符串被补偶数次,则得到原始字符串。
  • 因此,这些操作仅取决于K奇偶
  • 因此,我们将计算要执行的反向操作的次数。如果奇偶校验是奇数,那么我们将反转它。否则字符串将保持不变。
  • 同样,我们将计算要执行的补码操作的次数。如果奇偶校验是奇数,那么我们将补充它。否则字符串将保持不变。

下面是上述方法的实现:

C++
// C++ program to perform K operations upon
// the string and find the modified string
#include 
using namespace std;
 
// Function to perform K operations upon
// the string and find modified string
string ReverseComplement(
    string s, int n, int k)
{
 
    // Number of reverse operations
    int rev = (k + 1) / 2;
 
    // Number of complement operations
    int complment = k - rev;
 
    // If rev is odd parity
    if (rev % 2)
        reverse(s.begin(), s.end());
 
    // If complment is odd parity
    if (complment % 2) {
        for (int i = 0; i < n; i++) {
            // Complementing each position
            if (s[i] == '0')
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
 
    // Return the modified string
    return s;
}
 
// Driver Code
int main()
{
    string str = "10011";
    int k = 5;
    int n = str.size();
 
    // Function call
    cout << ReverseComplement(str, n, k);
 
    return 0;
}


Java
// Java program to perform K operations upon
// the String and find the modified String
class GFG{
  
// Function to perform K operations upon
// the String and find modified String
static String ReverseComplement(
    char []s, int n, int k)
{
  
    // Number of reverse operations
    int rev = (k + 1) / 2;
  
    // Number of complement operations
    int complment = k - rev;
  
    // If rev is odd parity
    if (rev % 2 == 1)
        s = reverse(s);
  
    // If complment is odd parity
    if (complment % 2 == 1) {
        for (int i = 0; i < n; i++) {
            // Complementing each position
            if (s[i] == '0')
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
  
    // Return the modified String
    return String.valueOf(s);
}
 
static char[] reverse(char a[]) {
    int i, n = a.length;
    char t;
    for (i = 0; i < n / 2; i++) {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "10011";
    int k = 5;
    int n = str.length();
  
    // Function call
    System.out.print(ReverseComplement(str.toCharArray(), n, k));
  
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to perform K operations upon
# the string and find the modified string
 
# Function to perform K operations upon
# the string and find modified string
def ReverseComplement(s,n,k):
    # Number of reverse operations
    rev = (k + 1) // 2
 
    # Number of complement operations
    complment = k - rev
 
    # If rev is odd parity
    if (rev % 2):
        s = s[::-1]
 
    # If complment is odd parity
    if (complment % 2):
        for i in range(n):
            # Complementing each position
            if (s[i] == '0'):
                s[i] = '1'
            else:
                s[i] = '0'
 
    # Return the modified string
    return s
 
# Driver Code
if __name__ == '__main__':
    str1 = "10011"
    k = 5
    n = len(str1)
 
    # Function call
    print(ReverseComplement(str1, n, k))
 
# This code is contributed by Surendra_Gangwar


C#
// C# program to perform K operations upon
// the String and find the modified String
using System;
class GFG{
     
// Function to perform K operations upon
// the String and find modified String
static string ReverseComplement(char []s,
                                int n, int k)
{
     
    // Number of reverse operations
    int rev = (k + 1) / 2;
     
    // Number of complement operations
    int complment = k - rev;
     
    // If rev is odd parity
    if (rev % 2 == 1)
        s = reverse(s);
     
    // If complment is odd parity
    if (complment % 2 == 1)
    {
        for (int i = 0; i < n; i++)
        {
            // Complementing each position
            if (s[i] == '0')
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
     
    // Return the modified String
    return (new string(s));
}
 
static char[] reverse(char[] a)
{
    int i, n = a.Length;
    char t;
    for (i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Driver Code
public static void Main()
{
    string str = "10011";
    int k = 5;
    int n = str.Length;
     
    // Function call
    Console.Write(ReverseComplement(str.ToCharArray(), n, k));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
11001

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live