String obtained by reversing and complementing a Binary string K times
Last Updated :
16 Nov, 2022
Given a binary string of size N and an integer K, the task is to perform K operations upon the string and print the final string:
- If the operation number is odd, then reverse the string,
- If the operation number even, then complement the string.
Examples:
Input: str = "1011", K = 2
Output: 0010
After the first step, string will be reversed and becomes "1101".
After the second step, the string will be complemented and becomes "0010".
Input: str = "1001", K = 4
Output: 1001
After all operation the string will remain same.
Naive Approach:
Traverse for all K steps and if the current step is odd then perform the reverse operation, otherwise complement the string.
Efficient Approach: Upon observing the given operation pattern:
- If a string is reversed even number of times, the original string is obtained.
- Similarly, if a string is complemented even number of times, the original string is obtained.
- Therefore, these operations depends only upon the parity of K.
- So we will count the number of reverse operations to be performed. If parity is odd, then we will reverse it. Else the string will remain unchanged.
- Similarly we will count the number of complement operations to be performed. If parity is odd, then we will complement it. Else the string will remain unchanged.
Below is the implementation of the above approach:
C++
// C++ program to perform K operations upon
// the string and find the modified string
#include <bits/stdc++.h>
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 complement = k - rev;
// If rev is odd parity
if (rev % 2)
reverse(s.begin(), s.end());
// If complement is odd parity
if (complement % 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 complement = k - rev;
// If rev is odd parity
if (rev % 2 == 1)
s = reverse(s);
// If complement is odd parity
if (complement % 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
complement = k - rev
# If rev is odd parity
if (rev % 2):
s = s[::-1]
# If complement is odd parity
if (complement % 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 complement = k - rev;
// If rev is odd parity
if (rev % 2 == 1)
s = reverse(s);
// If complement is odd parity
if (complement % 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
<script>
// Javascript 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
function ReverseComplement( s, n, k)
{
// Number of reverse operations
var rev = parseInt((k + 1) / 2);
// Number of complement operations
var complement = k - rev;
// If rev is odd parity
if (rev % 2)
{
s = s.split('').reverse().join('');
}
// If complement is odd parity
if (complement % 2) {
for (var 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
var str = "10011";
var k = 5;
var n = str.length;
// Function call
document.write( ReverseComplement(str, n, k));
// This code is contributed by famously.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Print string after removing all (â10â or â01â) from the binary string Given a binary string str consisting of only 0's and 1's, the task is to print the string after removing the occurrences of "10" and "01" from the string one by one. Print -1 if the string becomes null. Examples: Input: str = "101100" Output: -1 Explanation: In the first step, "10" at index 0 and 1
9 min read
Minimize cost to sort Binary String by swapping pairs or reversing prefix at most once Given binary string S of length N, the task is to minimize cost to sort the binary string using the following operations: Operation-1: Take any two indices i and j such that 0 ? i, j < N and swap Si and Sj. This operation is performed any number of times. Its cost is 1.Operation-2: Take any prefi
5 min read
Check if an encoding represents a unique binary string Given an encoding of a binary string of length k, the task is to find if the given encoding uniquely identifies a binary string or not. The encoding has counts of contiguous 1s (separated by 0s). For example, encoding of 11111 is {5}, encoding of 01101010 is {2, 1, 1} and encoding of 111011 is {3, 2
6 min read
Minimum substring reversals required to make given Binary String alternating Given a binary string S of length N, the task is to count the minimum number substrings of S that is required to be reversed to make the string S alternating. If it is not possible to make string alternating, then print "-1". Examples: Input: S = "10001110"Output: 2Explanation:In the first operation
7 min read
Count of Reverse Bitonic Substrings in a given String Given a string S, the task is to count the number of Reverse Bitonic Substrings in the given string. Reverse bitonic substring: A string in which the ASCII values of the characters of the string follow any of the following patterns: Strictly IncreasingStrictly decreasingDecreasing and then increasin
8 min read
Find Binary string by converting all 01 or 10 to 11 after M iterations Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0's to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.Note: 2?N?103, 1?M?109 Examples: Input: str="01100", M=1Ou
8 min read