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++
#include <bits/stdc++.h>
using namespace std;
string ReverseComplement(
string s, int n, int k)
{
int rev = (k + 1) / 2;
int complement = k - rev;
if (rev % 2)
reverse(s.begin(), s.end());
if (complement % 2) {
for ( int i = 0; i < n; i++) {
if (s[i] == '0' )
s[i] = '1' ;
else
s[i] = '0' ;
}
}
return s;
}
int main()
{
string str = "10011" ;
int k = 5;
int n = str.size();
cout << ReverseComplement(str, n, k);
return 0;
}
|
Java
class GFG{
static String ReverseComplement(
char []s, int n, int k)
{
int rev = (k + 1 ) / 2 ;
int complement = k - rev;
if (rev % 2 == 1 )
s = reverse(s);
if (complement % 2 == 1 ) {
for ( int i = 0 ; i < n; i++) {
if (s[i] == '0' )
s[i] = '1' ;
else
s[i] = '0' ;
}
}
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;
}
public static void main(String[] args)
{
String str = "10011" ;
int k = 5 ;
int n = str.length();
System.out.print(ReverseComplement(str.toCharArray(), n, k));
}
}
|
Python3
def ReverseComplement(s,n,k):
rev = (k + 1 ) / / 2
complement = k - rev
if (rev % 2 ):
s = s[:: - 1 ]
if (complement % 2 ):
for i in range (n):
if (s[i] = = '0' ):
s[i] = '1'
else :
s[i] = '0'
return s
if __name__ = = '__main__' :
str1 = "10011"
k = 5
n = len (str1)
print (ReverseComplement(str1, n, k))
|
C#
using System;
class GFG{
static string ReverseComplement( char []s,
int n, int k)
{
int rev = (k + 1) / 2;
int complement = k - rev;
if (rev % 2 == 1)
s = reverse(s);
if (complement % 2 == 1)
{
for ( int i = 0; i < n; i++)
{
if (s[i] == '0' )
s[i] = '1' ;
else
s[i] = '0' ;
}
}
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;
}
public static void Main()
{
string str = "10011" ;
int k = 5;
int n = str.Length;
Console.Write(ReverseComplement(str.ToCharArray(), n, k));
}
}
|
Javascript
<script>
function ReverseComplement( s, n, k)
{
var rev = parseInt((k + 1) / 2);
var complement = k - rev;
if (rev % 2)
{
s = s.split( '' ).reverse().join( '' );
}
if (complement % 2) {
for ( var i = 0; i < n; i++)
{
if (s[i] == '0' )
s[i] = '1' ;
else
s[i] = '0' ;
}
}
return s;
}
var str = "10011" ;
var k = 5;
var n = str.length;
document.write( ReverseComplement(str, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...