Python3 Program for Queries for Rotation and Kth Character of the given String in Constant Time


In this problem, we need to perform the queries according to the given rules and print the characters from the updated string. We will solve the problem using two different approach. In the first approach, we will use the string rotation concept, and in the second approach, we will use the index customization approach.

Problem statement – The task given to us is that perform given queries on the string. We have given the array of queries, and each query contains two integers.

Follow the below statements to perform the given queries on the string.

  • (1, l) – We need to the left rotate the string for l times.

  • (2, l) – We need to access the character from the lth position. Here, 1 <= l <= n.

Sample examples

Input

queries = [[1, 3], [2, 3], [2, 4], [2, 2]], s = "vdflkmng"

Output

m, n, k

Explanation – Let’s perform all four queries given in the array.

  • Make 3 left rotations of the string in the first query. The final string will be ‘lkmngvdf’.

  • Print the third character, which is ‘m’.

  • Print the fourth character, which is ‘n’.

  • Print the second character, which is ‘k’.

Input

queries = [[1, 3], [1, 2], [1, 3], [2, 2]], s = "welcome"

Output

l

Explanation

  • After the first query, the string will be ‘comewel’.

  • After the second query, the string will be ‘mewelco’.

  • After the third query, the string will be ‘elcomew’.

  • In the last query, it prints the 2nd character, which is ‘l’.

Approach 1

In this approach, we divide the given string into two parts and concatenate again to perform N left rotations. After that, we access the character from the given index in the second query.

Algorithm

Step 1 – In the first step, we require to iterate an array of all queries.

Step 2 – If que[m][0] == 1, make total que[m][1] left rotations of the string s and store it to s.

Step 2.1 – To make que[m][1] left rotations, we can divide the string from the m index into two parts and concatenate the right part and left part.

Step 3 – If que[m][0] == 2, we need to print the character from the if que[m][1] index.

Step 4 – Access the character from the given index and show it in the output.

Example

def executeQueries(s, str_len, que, que_len):
    # Traverse query
    for m in range(que_len):
        # String rotation
        if que[m][0] == 1:
            # Rotate the string by que[m][1] to the left
            s = s[que[m][1]:] + s[:que[m][1]]
        else:
            index = que[m][1] - 1
            # Show character
            print(s[index])
# Driver code
if __name__ == "__main__":
    s = "vdflkmng"
    length = len(s)
    queries = [[1, 3], [2, 3], [2, 4], [2, 2]]
    queries_length = len(queries)
    executeQueries(s, length, queries, queries_length)

Output

m
n
k

Time complexity – O(M*N) as we divide the string into two parts and traverse the list.

Space complexity – O(N), as we store the rotational string.

Approach 2

This approach is the optimized version of the above approach. Here, we make a pointer to point to the last index. When we execute the query, we update the pointer value, which helps us to save time and space.

Algorithm

Step 1 – Define the ‘ind_ptr’ variable and initialize with zero, representing the index pointer.

Step 2 – Use the loop to traverse the list.

Step 3 – If que[m][0] is equal to 1, we need to manipulate the index pointer value.

Step 3.1 – Add que[m][1] to the ind_ptr, take the modulo with 26 of the resultant value, and assign it again to the ind_ptr variable.

Step 4 – If que[m][0] is equal to 2, we need to print the character which is at the que[m][1] index.

Step 5 – Add que[m][1] – 1 to the ind_ptr variable, and take its modulo with 26. After that, assign the resultant value to the index variable.

Step 6 – Access the character from the index and print it to show in the output.

Example

def executeQueries(s, str_len, que, que_len):
    # Starting pointer
    ind_ptr = 0
    # Traverse query
    for m in range(que_len):
        # String rotation
        if que[m][0] == 1:
            # Change index pointer value
            ind_ptr = (ind_ptr + que[m][1]) % str_len
        else:
            index = que[m][1]
            # Get the rotational index of the character
            index = (ind_ptr + index - 1) % str_len
            # Show character
            print(s[index])
if __name__ == "__main__":
    s = "vdflkmng"
    length = len(s)
    queries = [[1, 3], [2, 3], [2, 4], [2, 2]]
    queries_length = len(queries)
    executeQueries(s, length, queries, queries_length)

Output

m
n
k

Time complexity – O(M) as we iterate through the given list of queries.

Space complexity – O(1) as we use constant space.

We perform the left rotations of the string according to the given conditions. Programmers can also try to perform queries containing the right string rotation.

Updated on: 25-Aug-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements