Reverse String Operations Problem Statement

You are provided with a string S and an array of integers A of size M. Your task is to perform M operations on the string as specified by the indices in array A.

The operations should be executed in the sequence they are listed in array A. During the ith operation, you reverse the substring of S starting from position A[i] to len(S) - A[i] - 1 (0-based index).

Your goal is to determine the string after executing all operations.

Example:

Input:
'S' = "aabcd", 'M' = 2, 'A' = [0, 1]
Output:
"dabca"
Explanation:

After the 1st operation (reverse from [0, 4]), 'S' becomes "dcbaa". After the 2nd operation (reverse from [1, 3]), 'S' becomes "dabca". Hence, the resulting string is "dabca".

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ len(S) ≤ 105
  • 1 ≤ M ≤ 105
  • Each A[i] guarantees the range [A[i], len(S) - A[i] - 1] is non-empty.
  • The sum of lengths of S and M is ≤ 105 for all test cases combined.
  • Time limit: 1 sec
Be the first one to answer
Add answer anonymously...
Tech Mahindra SDE-2 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

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