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.
Input: str = “1011”, K = 2
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
After all operation the string will remain same.
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:
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Longest sub string of 0's in a binary string which is repeated K times
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Find i'th Index character in a binary string obtained after n iterations
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
- Check if any permutation of string is a K times repeated string
- Count of times second string can be formed from the characters of first string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Check if string can be made lexicographically smaller by reversing any substring
- Lexicographically smallest string obtained after concatenating array
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Largest sub-string of a binary string divisible by 2
- Python | Check if a given string is binary string or not
- Print string after removing all (“10” or “01”) from the binary string
- Largest sub-string where all the characters appear at least K times
- String which when repeated exactly K times gives a permutation of S
- Number of subsequences as "ab" in a string repeated K times
- C++ program to concatenate a string given number of times
- Encrypt a string by repeating i-th character i times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.