Queries for rotation and Kth character of the given string in constant time
Last Updated :
01 Jun, 2022
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define size 2
void performQueries(string str, int n,
int queries[][size], int q)
{
int ptr = 0;
for (int i = 0; i < q; i++) {
if (queries[i][0] == 1) {
ptr = (ptr + queries[i][1]) % n;
}
else {
int k = queries[i][1];
int index = (ptr + k - 1) % n;
cout << str[index] << "\n";
}
}
}
int main()
{
string str = "abcdefgh";
int n = str.length();
int queries[][size] = { { 1, 2 }, { 2, 2 },
{ 1, 4 }, { 2, 7 } };
int q = sizeof(queries) / sizeof(queries[0]);
performQueries(str, n, queries, q);
return 0;
}
Java
import java.util.*;
class GFG
{
static int size = 2;
static void performQueries(String str, int n,
int queries[][], int q)
{
int ptr = 0;
for (int i = 0; i < q; i++)
{
if (queries[i][0] == 1)
{
ptr = (ptr + queries[i][1]) % n;
}
else
{
int k = queries[i][1];
int index = (ptr + k - 1) % n;
System.out.println(str.charAt(index));
}
}
}
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);
}
}
Python3
size = 2
def performQueries(string, n, queries, q) :
ptr = 0;
for i in range(q) :
if (queries[i][0] == 1) :
ptr = (ptr + queries[i][1]) % n;
else :
k = queries[i][1];
index = (ptr + k - 1) % n;
print(string[index]);
if __name__ == "__main__" :
string = "abcdefgh";
n = len(string);
queries = [[ 1, 2 ], [ 2, 2 ],
[ 1, 4 ], [ 2, 7 ]];
q = len(queries);
performQueries(string, n, queries, q);
C#
using System;
class GFG
{
static int size = 2;
static void performQueries(String str, int n,
int [,]queries, int q)
{
int ptr = 0;
for (int i = 0; i < q; i++)
{
if (queries[i, 0] == 1)
{
ptr = (ptr + queries[i, 1]) % n;
}
else
{
int k = queries[i, 1];
int index = (ptr + k - 1) % n;
Console.WriteLine(str[index]);
}
}
}
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.GetLength(0);
performQueries(str, n, queries, q);
}
}
Javascript
<script>
var size = 2;
function performQueries(str, n, queries, q)
{
var ptr = 0;
for (var i = 0; i < q; i++) {
if (queries[i][0] == 1) {
ptr = (ptr + queries[i][1]) % n;
}
else {
var k = queries[i][1];
var index = (ptr + k - 1) % n;
document.write( str[index] + "<br>");
}
}
}
var str = "abcdefgh";
var n = str.length;
var queries = [ [ 1, 2 ], [ 2, 2 ],
[ 1, 4 ], [ 2, 7 ] ];
var q = queries.length;
performQueries(str, n, queries, q);
</script>
Time Complexity: O(Q), Where Q is the number of queriesAuxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...