Reverse String

You are given a string ‘S’. You are also given ‘M’ integers in an array ‘A’. You perform ‘M’ operations on this string. The operations are given in an array ‘A’ of size ‘M’.

You perform the operations in the order they appear in the array ‘A’. In the ‘i’th operation, you reverse the substring of ‘S’ from the position ‘A[i]’ to ‘len(S)’ - ‘A[i]’ - 1 (0 based).

Your task is to find the string after performing all the operations.

Example :
‘S’ = “aabcd”, ‘M’ = 2, ‘A’ = [0, 1]
After 1st operation i.e, reversing from [0, 4], ‘S’ = “dcbaa”.
After 2nd operation i.e, reversing from [1, 3], ‘S’ = “dabca”.
Hence, the answer is “dabca”.
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.

The first line of each test case contains a string ‘S’ consisting of lowercase English characters without spaces.

The second line of each test case contains a single integer ‘M’ denoting the number of operations.

The third line of each test case contains ‘M’ space-separated integers denoting the elements of the array ‘A’.
Output Format :
For each test case, find the string after performing all the operations.

Output for each test case will be printed on a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 ≤ T ≤ 10
1 ≤ len(S) ≤ 10^5
1 ≤ M ≤ 10^5
Each ‘A[i]’ is such that the range [‘A[i]’, len(‘S’) - ‘A[i]’ - 1] is non-empty.
It is guaranteed that the sum of lengths of ‘S’ and ‘M’ is ≤ 10^5 for all test cases, respectively.

Time limit: 1 sec
CodingNinjas
author
2y

Started loop from end and stored in another variable

CodingNinjas
author
2y
Optimal Approach

Approach:

  • Let ‘n’ be the length of string ‘S’. First, we need to make an important observation. An element at position ‘i’ can either remain at the position ‘i’ or position ‘n’ - ‘i’...read more
Help your peers!
Add answer anonymously...
Accenture Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter