Java Program for Queries for rotation and Kth character of the given string in constant time Last Updated : 09 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 = "ghabcdef" Query 4: 7th character is eInput: str = "abc", q[][] = {{1, 2}, {2, 2}} Output: a 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 queriesAuxiliary Space: O(1) Please refer complete article on Queries for rotation and Kth character of the given string in constant time for more details! Comment More infoAdvertise with us Next Article Java Program for Queries for rotation and Kth character of the given string in constant time kartik Follow Improve Article Tags : Strings Java Java Programs DSA rotation +1 More Practice Tags : JavaStrings Similar Reads Java Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3}Â Output: 2Â Explanation:Â Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2}Â Output: 1Â Explanation:Â So 4 min read Java Program to Find the Mth element of the Array after K left rotations Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation:Â The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ] 3 min read Java Program to Find Range sum queries for anticlockwise rotations of Array by K indices Given an array arr consisting of N elements and Q queries of the following two types: 1 K: For this type of query, the array needs to be rotated by K indices anticlockwise from its current state.2 L R: For this query, the sum of the array elements present in the indices [L, R] needs to be calculated 4 min read How to Check if One String is a Rotation of Another in Java? In Java, to check if one string is a rotation of another, we can do string concatenation and substring matching. A rotated string is created by moving some characters from the beginning of the string to the end while maintaining the character order.Program to Check if One String is a Rotation of Ano 2 min read Java Program to Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc 3 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 <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : 6 min read Java Program for Minimum rotations required to get the same string Given a string, we need to find the minimum number of rotations required to get the same string. Examples: Input : s = "geeks"Output : 5 Input : s = "aaaa"Output : 1 The idea is based on below post. A Program to check if strings are rotations of each other or not Step 1 : Initialize result = 0 (Here 2 min read Java Program to Count rotations in sorted and rotated linked list Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k. The idea is to traverse singly linked list to check condition whether current node value is greater than value of next node. If the given condition is true, then break the loop. Otherwise increase 3 min read Java Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read Java Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 6 min read Like