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


In this problem, programmers require to execute the queries on the string. Also, need to rotate the string and print the characters of the updated string. The best approach to solve the problem is that keep updating the index value and access string characters when we need to print the character.

Problem statement – We have given the string alpha and the array containing the pair of numbers named ‘que’. The task is that perform the queries given in the array onto the string alpha.

Follow the below query operation rules.

  • (1, a) – Make total left rotations of the given string.

  • (2, a) – Print the character from the ath position.

Note – Here, the value of a is 1 to N, where N is the length of the string.

Sample examples

Input

que[][] = { { 1, 1 }, { 2, 1 }, { 2, 5 }, { 1, 3 } }, alpha = "wqedsdcs";

Output

q, d

Explanation – Let’s perform all queries on the string one by one.

  • The first query rotates the string by 1, and the updated string will be ‘qedsdcsw’.

  • The second query prints the character from the 1st position, which is ' q’.

  • The third query prints the character from the 5th position, which is ‘d’.

  • The fourth query makes 3 left rotations.

Input

que[][] = { { 1, 1 }, { 2, 3 }, { 1, 8 }, { 2, 7 } }, alpha = "tutorialspoint"

Output

o, u

Explanation

  • After performing the first query string becomes ‘utorialspoint’.

  • The character at the 3rd position in the updated string is ‘o’.

  • After performing the third query, the string becomes ‘pointtutorials’.

  • The character at 7th position is ‘u’.

Approach 1

In this approach, we define the index pointer to track the current index. We keep track of the last index to execute the next query on the last index.

Algorithm

Step 1 – Define the ‘curr’ variable to keep track of the latest index.

Step 2 – Use the for loop to traverse the array of queries.

Step 3 – Get the query from the mth index, and if the first element of the query pair is 1, we need to make a total ‘a’ number of rotations of the lastly updated string.

Step 4 – In this case, we update the ‘curr’ value by adding the second element of the pair to ‘curr’ and taking its modulo with 26.

Step 5 – If the first element of the query pair is 2, we need to print the character from the given index.

Step 6 – Update the ‘curr’ variable’s value by adding the second element of the query pair to ‘curr’ and taking its modulo with 26.

Step 7 – Access the character from the ‘curr’ index in the original string. Also, show the character in the output.

Example

import java.util.*;

public class main {
    static void executeQueries(String alpha, int str_len, int que[][], int que_len) {
        // Current pointer pointing to the starting
        int curr = 0;
        // Traverse array
        for (int m = 0; m < que_len; m++) {
            // Query rotation
            if (que[m][0] == 1) {
                // Update value of curr
                curr = (curr + que[m][1]) % str_len;
            } else {
                int ch = que[m][1];
                // Get the index of the character in rotation
                int index = (curr + ch - 1) % str_len;
                // Show output
                System.out.println(alpha.charAt(index));
            }
        }
    }
    public static void main(String[] args) {
        String alpha = "wqedsdcs";
        int str_len = alpha.length();
        int que[][] = { { 1, 1 }, { 2, 1 },
                { 2, 5 }, { 1, 3 } };
        int que_len = que.length;
        executeQueries(alpha, str_len, que, que_len);
    }
}

Output

q
d

Time complexity – O(M), as we perform all queries one by one.

Space complexity – O(1) as we don’t use any dynamic space.

Programmers can also try to solve the problem using the naïve approach. In the naïve approach, programmers can rotate the string to execute the first query and access characters from the updated string to execute the second query. There are 1 to 2 different ways to perform the string rotation; one is to concatenate the string to itself and take its substring.

Updated on: 24-Aug-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements