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 e
Input: 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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define size 2
// Function to perform the required
// queries on the given string
void performQueries(string str, int n,
int queries[][size], 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
cout << str[index] << "\n";
}
}
}
// Driver code
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
// 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
Python3
# Python3 implementation of the approach
size = 2
# Function to perform the required
# queries on the given string
def performQueries(string, n, queries, q) :
# Pointer pointing to the current starting
# character of the string
ptr = 0;
# For every query
for i in range(q) :
# 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 :
k = queries[i][1];
# Index of the kth character in the
# current rotation of the string
index = (ptr + k - 1) % n;
# Print the kth character
print(string[index]);
# Driver code
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);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
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
Console.WriteLine(str[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.GetLength(0);
performQueries(str, n, queries, q);
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript implementation of the approach
var size = 2;
// Function to perform the required
// queries on the given string
function performQueries(str, n, queries, q)
{
// Pointer pointing to the current starting
// character of the string
var ptr = 0;
// For every query
for (var 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 {
var k = queries[i][1];
// Index of the kth character in the
// current rotation of the string
var index = (ptr + k - 1) % n;
// Print the kth character
document.write( str[index] + "<br>");
}
}
}
// Driver code
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 queries
Auxiliary Space: O(1)
Similar Reads
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
2 min read
Kth character from the Nth string obtained by the given operations Given two positive integers N and K, the task is to find the Kth character of a string obtained by performing the following operation on a string S( initially "A") N times. Every ith operation generates following string (Si): Si = Si - 1 + 'B' + rev(comp(Si - 1)) where, comp() denotes the complement
6 min read
Number of sub-strings that contain the given character exactly k times Given the string str, a character c, and an integer k > 0. The task is to find the number of sub-strings that contain the character c exactly k times.Examples: Input: str = "abada", c = 'a', K = 2 Output: 4 All possible sub-strings are "aba", "abad", "bada" and "ada". Input: str = "55555", c = '5
10 min read
Count of substrings of given string with frequency of each character at most K Given a string str, the task is to calculate the number of substrings of the given string such that the frequency of each element of the string is almost K. Examples: Input: str = "abab", K = 1Output: 7Explanation: The substrings such that the frequency of each character is atmost 1 are "a", "b", "a
6 min read
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
8 min read