Open In App

Java Program for Queries for rotation and Kth character of the given string in constant time

Last Updated : 09 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string str, the task is to perform the following type of queries on the given string: 
 

  1. (1, K): Left rotate the string by K characters.
  2. (2, K): Print the Kth character of the string.

Examples: 
 

Input: str = “abcdefgh”, q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} 
Output: 


Query 1: str = “cdefghab” 
Query 2: 2nd character is d 
Query 3: str = “ghabcdef” 
Query 4: 7th character is e
Input: str = “abc”, q[][] = {{1, 2}, {2, 2}} 
Output: 

 

 

Approach: The main observation here is that the string doesn’t need to be rotated in every query instead we can create a pointer ptr pointing to the first character of the string and which can be updated for every rotation as ptr = (ptr + K) % N where K the integer by which the string needs to be rotated and N is the length of the string. Now for every query of the second type, the Kth character can be found by str[(ptr + K – 1) % N].
Below is the implementation of the above approach: 
 

Java




// Java implementation of the above approach
import java.util.*;
class GFG
{
static int size = 2;
 
// Function to perform the required
// queries on the given string
static void performQueries(String str, int n,
                           int queries[][], int q)
{
 
    // Pointer pointing to the current
    // starting character of the string
    int ptr = 0;
 
    // For every query
    for (int i = 0; i < q; i++)
    {
 
        // If the query is to rotate the string
        if (queries[i][0] == 1)
        {
 
            // Update the pointer pointing to the
            // starting character of the string
            ptr = (ptr + queries[i][1]) % n;
        }
        else
        {
            int k = queries[i][1];
 
            // Index of the kth character in the
            // current rotation of the string
            int index = (ptr + k - 1) % n;
 
            // Print the kth character
            System.out.println(str.charAt(index));
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    String str = "abcdefgh";
    int n = str.length();
 
    int queries[][] = { { 1, 2 }, { 2, 2 },
                        { 1, 4 }, { 2, 7 } };
    int q = queries.length;
 
    performQueries(str, n, queries, q);
}
}
 
// This code is contributed by 29AjayKumar


Output: 

d
e

 

Time Complexity: O(Q) , Where Q is the number of queries
Auxiliary Space: O(1)

Please refer complete article on Queries for rotation and Kth character of the given string in constant time for more details!



Similar Reads

C++ Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
3 min read
Python3 Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
2 min read
Javascript Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
2 min read
Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
6 min read
Java Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
6 min read
C++ Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
5 min read
Python3 Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
Javascript Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n) Right (Or clockwise) rotate the given string by d elements (where d &lt;= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "
18 min read
Kth character after replacing each character of String by its frequency exactly X times
Given a string S consisting of N digits from [1, 9] and a positive integer K and X. Every day each character of the string is replaced by its frequency and value. The task is to find the Kth character of the string after X days. Examples: Input: S = "1214", K = 10, X = 3Output: 4Explanation:1st day = "12214444"2nd day = “1222214444444444444444”3rd
8 min read
Article Tags :
Practice Tags :