
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for Queries for Rotation and Kth Character of Given String in Constant Time
In this problem, we need to perform the given queries on the string. We can solve the problem by making the rotations of the string differently and accessing the required characters using their index.
Problem statement - We have given string str of length N and array ?que' of size M containing queries. We need to perform the queries given in the array according to the below conditions.
(1, x) - Make x left rotations of the string.
(2, x) - Show the xth character in the output.
Sample examples
Input
que[][2] = {{1, 2}, {2, 1}, {1, 1}, {2, 3}}, str = "ersfd5tr"
Output
s, 5
Explanation
After performing the first query, the string becomes ?sfd5trer'.
In the second query, it shows the character which is at 1st position in the updated string. So, the character is ?s'.
In the third query, it makes 1 left rotation, and the resultant string will be ?fd5trers'.
The last query shows the character at the 3rd position, which is 5.
Input
que[][2] = {{1, 5}, {2, 1}, {2, 2}, {2, 3}}, str = "tutorialspoint"
Output
i, a, l
Explanation
After performing the first query, the string becomes ?ialspointtutor'.
In the second query, it shows ?i'.
The third query shows ?a', and the fourth query shows ?l'.
Approach 1
In this approach, we will rotate the string if we get (1, x) query pair. We will use the substr() method to get the substring and rotate the given string from xth index.
Algorithm
Step 1 - Start traversing the array of queries.
Step 2 - If the first element in the query pair is 1, make x left rotations of the string and update the string. We can take the right part of the string and the left part of the string. After that, we can merge right + left to rotate the string.
Step 3 - If the first element in the query pair is 2, access the character index from the query pair.
Step 4 - Print the character of the string by accessing it via the given index.
Example
#include <bits/stdc++.h> using namespace std; void executeQueries(string str, int str_len, int que[][2], int q_size){ // Traverse query for (int p = 0; p < q_size; p++) { // For string rotation if (que[p][0] == 1) { // rotating str by que[p][1] str = str.substr(que[p][1], str_len - que[p][1]) + str.substr(0, que[p][1]); } else { int x = que[p][1]; // Show character in the output cout << str[x - 1] << endl; ; } } } int main() { string str = "ersfd5tr"; int str_len = str.length(); int que[][2] = {{1, 2}, {2, 1}, {1, 1}, {2, 3}}; int q_size = sizeof(que) / sizeof(que[0]); executeQueries(str, str_len, que, q_size); return 0; }
Output
s 5
Time complexity - O(M*N), as we traverse the query array and take substring inside that.
Space complexity - O(N) as we store the substring.
Approach 2
In this approach, we manipulate the index value by updating it according to the given query. When we need to print the character, we access the character according to the updated index value.
Algorithm
Step 1 - Define the ?ind' variable and initialize with 0 to store the updated index.
Step 2 - While traversing the queries, if we need to rotate the string, update the ?ind' value by adding total rotations and taking its modulo with 26.
Step 3 - If we require to print the character, we can add the ?x' value to the ?ind' value and take its modulo with 26. After getting the updated index, we can print the character.
Example
#include <bits/stdc++.h> using namespace std; void executeQueries(string str, int str_len, int que[][2], int q_size) { // Starting x_ind int ind = 0; // Traverse query for (int p = 0; p < q_size; p++) { // For string rotation if (que[p][0] == 1) { // Change x_ind according to the array element value ind = (ind + que[p][1]) % str_len; } else { int x = que[p][1]; // Find the index of X in the current rotation int x_ind = (ind + x - 1) % str_len; // Show character in the output cout << str[x_ind] << endl; } } } int main() { string str = "ersfd5tr"; int str_len = str.length(); int que[][2] = {{1, 2}, {2, 1}, {1, 1}, {2, 3}}; int q_size = sizeof(que) / sizeof(que[0]); executeQueries(str, str_len, que, q_size); return 0; }
Output
s 5
Time complexity - O(M), as we traverse the queries.
Space complexity - O(1), as we don't use any extra space.
We learned two approaches to solving the problem. The first approach makes the rotations of the string and accesses the string character in the rotated string. It has more time and space complexity. The second approach is the optimized version of the first approach, which manipulates the index and accesses the character from the original string.