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

Rotation of the given string means moving the characters of the given string in clockwise or anticlockwise manner by some indexes. Here, we will implement a JavaScript program for queries for rotation and kth character of the given string in the constant time. In this article we will implement the proper code and explain each step.

## Introduction to Problem

In the given problem, we are given by a string which contains some characters in it. We are given by some limited queries and each query contains two numbers or integers. First integer represents the number of times we have to rotate the string (in this problem we are just rotating the string in the clockwise manner or right rotation of the string), while the second integer represent the character which is present at the given value after the first integer number of right rotation, that character we have to return.

For example −

Given string: "abcdef"
Query1: 3 4
Result: 3rd rotation of the above string is: defabc, character at 4th index is: 'b'.
Query2: 6 2
Result: 6th rotation of the above string is: abcdef, character at 2nd index is: 'c'.


We have seen the examples, now let us move to the approach and the code implementation part

## Approach

In this problem, first we have to rotate the string in given number of times and then we have to find the integer at the given index.

Before moving to the approach, we have to discuss some points −

• In this problem the rotation of string direction is not suggest that is either string will be rotated in the left direction or in the right direction. So, we will assume the right rotation.

• If we rotate any array or the string its length number of times, so we will get the exact same array back. So which means if we take the mode of the integer given for the rotation with the length of the string gives us the same number of rotation that were required.

• In the problem it is given that we have to give the solution in O(1) time of the constant time, so we will implement only efficient approach and give a simple idea about the naive one.

In the naive approach, we can take the mode of the current given number for rotation with the size of the string and get that rotation and return the required index character as the result. But this will make the time complexity of the given code as O(Q*N), where Q is the number of queries and N is the size of the string.

## Efficient Approach

If we check mathematically, every rotation could be found by adding the same string after the given string. For example −

If the given string is: “abcdef” and we add the same string after it result will be: abcdefabcdef, which means after each complete rotation we will get a new string in between the indexes. So we can answer each query in O(1) by using the mathematical formula.

### Example

// function to answer queries
function valueAtIndex(str, rotation, position){

// getting size of the given string
var n = str.length

// actual posisiton is
var pos = (position + rotation) %n;
console.log("Character present at index " + position + " after " + rotation + " number of rotations is: " + str[pos]);
}

// defining the string
var str = "abcdef"

// quries array
var queries = [[3,4], [6,2]];

// travesing over the queries
for(var i =0; i < queries.length; i++){
valueAtIndex(str, queries[i], queries[i]);
}


### Time and Space Complexity

The time complexity of the above code is O(Q), where Q is the number of queries asked because answer to each query is given in O(1). The space complexity of the above code is O(1) as we have not used any extra space in the above code.

## Conclusion

In this tutorial, we have implemented a JavaScript program for queries for rotation and kth character of the given string in the constant time. We have generated a mathematical concept by adding the same given string after the current one to answer all the queries in O(1) making the time complexity of the code as O(Q) and space complexity as O(1).

Updated on: 14-Apr-2023

63 Views 