Given a string S of size N and Q queries.Each query is either of the following types:
Type 1- Rotate the string by K character from the end.
Type 2- Print the character at the I'th index of the resulting string at that instant.
Answer all queries.
Example 1:
Input:
N=7 Q=5
S=abcdefg
Query_{1}: Type=1 K=2
Query_{2}: Type=2 I=0
Query_{3}: Type=2 I=6
Query_{4}: Type=1 K=4
Query_{5}: Type=2 I=1
Output:
f
e
c
Explanation:
After First query String becomes-"fgabcde"
Second query returns - 'f'
Third query returns -'e'
After Fourth Query String becomes-"bcdefga"
Fifth Query returns -'c'
Example 2:
Input:
N=5 Q=2
S=geeks
Query_{1}: Type=1 K=4
Query_{2}: Type=2 I=4
Output:
g
Explanation:
After first query,string becomes "eeksg".
Second query returns 'g'.
Your task:
You don't need to read input or print anything.Your task is to complete the function StringQuery() which takes an integer N,an integer Q, a string S and two arrays Q1 and Q2(containing Query type and K/I respectively) and returns an array containing the answers to the queries.
Expected Time Complexity:O(Q)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=10^{5}
1<=Q<=10^{4}
1<=Type<=2
1<=K<=10^{9}
0<=I

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Implementation

Starter code: #User function Template for python3



class Solution:

    def StringQuery(self,N,Q,S,Q1,Q2):

        #code here

Solutions:
class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		ans = []
		curr = 0
		for i in range(Q):
			if Q1[i] == 2:
				ans.append(S[(curr + Q2[i]) % N])
			else:
				curr = (curr + N - Q2[i]) % N
		return ans


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		x = 0
		ans = []
		for i in range(Q):
			if Q1[i] == 1:
				x = (x + Q2[i]) % N
			elif Q2[i] < x:
				ans.append(S[Q2[i] - x])
			else:
				ans.append(S[Q2[i] - x])
		return ans


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		output = []
		for i in range(Q):
			if Q1[i] == 1:
				j = Q2[i] % N
				S = S[-j:] + S[:-j]
			else:
				output.append(S[Q2[i]])
		return output


class Solution:

	def rotate(self, s, n):
		if not s or n == 0:
			return s
		n = n % len(s)
		return s[-n:] + s[:-n]

	def StringQuery(self, N, Q, S, Q1, Q2):
		l = []
		for i in range(Q):
			if Q1[i] == 1:
				S = self.rotate(S, Q2[i])
			else:
				l.append(S[Q2[i]])
		return l


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		ans = []
		for i in range(Q):
			if Q1[i] == 1:
				k = Q2[i] % len(S)
				temp = S[len(S) - k:] + S[:len(S) - k]
				S = temp
			else:
				ans.append(S[Q2[i]])
		return ans


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		ans = 0
		res = []
		for i in range(Q):
			if Q1[i] == 1:
				ans += N - Q2[i] % N
			else:
				res += [S[(Q2[i] + ans) % N]]
		return res


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		f = 0
		g = []
		for i in range(Q):
			if Q1[i] == 1:
				f = (f + (N - Q2[i])) % N
			else:
				g.append(S[(f + Q2[i]) % N])
		return g


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		result = []
		for i in range(Q):
			if Q1[i] == 2:
				result.append(S[Q2[i]])
			else:
				K = Q2[i] % len(S)
				S = S[-K:] + S[:-K]
		return result


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		l = []
		n = len(S)
		for i in range(Q):
			query = Q1[i]
			k = Q2[i]
			if query == 1:
				first = S[:(n - k) % n]
				second = S[(n - k) % n:]
				S = second + first
			else:
				l.append(S[k])
		return l


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		chars = []
		for i in range(Q):
			if Q1[i] == 1:
				K = Q2[i] % N
				S = S[N - K:] + S[0:N - K]
			else:
				chars.append(S[Q2[i]])
		return chars


class Solution:

	def StringQuery(self, N, Q, S, Q1, Q2):
		l = []
		k = S
		for i in range(Q):
			if Q1[i] == 1:
				p = Q2[i] % N
				x = k[:N - p]
				y = k[N - p:]
				z = y + x
				k = z
			elif Q1[i] == 2:
				p = Q2[i] % N
				l.append(k[p])
		return l