Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your task is to find the final String after B swaps.
Input : S = "ABCDEFGH", B = 4, C = 3; Output: DEFGBCAH Explanation: after 1st swap: DBCAEFGH after 2nd swap: DECABFGH after 3rd swap: DEFABCGH after 4th swap: DEFGBCAH Input : S = "ABCDE", B = 10, C = 6; Output : ADEBC Explanation: after 1st swap: BACDE after 2nd swap: BCADE after 3rd swap: BCDAE after 4th swap: BCDEA after 5th swap: ACDEB after 6th swap: CADEB after 7th swap: CDAEB after 8th swap: CDEAB after 9th swap: CDEBA after 10th swap: ADEBC
- For large values of B, the naive approach of looping B times, each time swapping ith character with (i + C)%N-th character will result in high CPU time.
- The trick to solving this problem is to observe the resultant string after every N iterations, where N is the length of the string S.
- Again, if C is greater than or equal to the N, it is effectively equal to the remainder of C divided by N.
- Hereon, let’s consider C to be less thanN.
- If we observe the string that is formed after every N successive iterations and swaps (let’s call it one full iteration), we can start to get a pattern.
- We can find that the string is divided into two parts: the first part of length C comprising of the first C characters of S, and the second part comprising of the rest of the characters.
- The two parts are rotated to the left by some places. The first part is rotated by (N % C) places every full iteration.
- The second part is rotated by C places every full iteration.
- We can calculate the number of full iterations f by dividing B by N.
- So, the first part will be rotated left by ( N % C ) * f . This value can go beyond C and so, it is effectively ( ( N % C ) * f ) % C, i.e. the first part will be rotated by ( ( N % C ) * f ) % C places left.
- The second part will be rotated left by C * f places. Since, this value can go beyond the length of the second part which is ( N – C ), it is effectively ( ( C * f ) % ( N – C ) ), i.e. the second part will be rotated by ( ( C * f ) % ( N – C ) ) places left.
- After f full iterations, there may still be some iterations remaining to complete B iterations. This value is B % N which is less than N. We can follow the naive approach on these remaining iterations after f full iterations to get the resultant string.
s = ABCDEFGHIJK; c = 4;
parts: ABCD EFGHIJK
after 1 full iteration: DABC IJKEFGH
after 2 full iteration: CDAB FGHIJKE
after 3 full iteration: BCDA JKEFGHI
after 4 full iteration: ABCD GHIJKEF
after 5 full iteration: DABC KEFGHIJ
after 6 full iteration: CDAB HIJKEFG
after 7 full iteration: BCDA EFGHIJK
after 8 full iteration: ABCD IJKEFGH
Below is the implementation of the approach:
Time Complexity: O(n)
Space Complexity: O(n)
- C program to swap adjacent characters of a String
- Swap all occurrences of two characters to get lexicographically smallest string
- Java program to swap first and last characters of words in a sentence
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Length of longest consecutive ones by at most one swap in a Binary String
- Find Kth largest string from the permutations of the string with two characters
- Find the smallest window in a string containing all characters of another string
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.
Improved By : 29AjayKumar