Given two positive integers N and K, the task is to find the Kth character of a string obtained by performing the following operation on a string S( initially “A”) N times.
Every ith operation generates following string (Si):
Si = Si – 1 + ‘B’ + rev(comp(Si – 1))
comp() denotes the complement of string i.e., A is changed to B and B is changed to A.
and rev() returns the reverse of a string.
Input: N = 3, K = 1
Initially, after first operation, S1 = “A”
After 2nd operation, S2 = “ABB”
After 3rd operation, S3 = “ABBBAAB”
The first character of S3 is ‘A’.
Input: N = 2, K = 3
Approach: The idea is to use recursion to generate the new string from the previously generated string and to repeat the process until N operations are performed. Below are the steps:
- Initialize two string prev as “A” and curr as an empty string.
- If N = 1 then returns prev otherwise perform below operation.
- Iterate a loop (N – 1) times, each time update curr as prev + “B”.
- Reverse the string prev.
- Again update the string curr string as curr + prev.
- Update string prev as curr.
- After the above steps return the (K – 1)th character of the string curr.
Below is the implementation of the above approach:
Time Complexity: O(N2)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find i'th Index character in a binary string obtained after n iterations
- Check if sum Y can be obtained from the Array by the given operations
- Queries for rotation and Kth character of the given string in constant time
- Kth most frequent Character in a given String
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Queries for Nth smallest character for a given range in a string
- Mth bit in Nth binary string from a sequence generated by the given operations
- Program to find the kth character after decrypting a string
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Print Kth character in sorted concatenated substrings of a string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- 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
- String obtained by reversing and complementing a Binary string K times
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Check if a string can be obtained by appending subsequences 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.