VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-modify-a-string-by-performing-given-shift-operations/

⇱ Java Program to Modify a string by performing given shift operations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Modify a string by performing given shift operations

Last Updated : 23 Jul, 2025

Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task is to return the modified string that can be obtained after performing the given operations.
Note: A left shift by 1 refers to removing the first character of S and append it to the end. Similarly, a right shift by 1 refers to removing the last character of S and insert at the beginning.

Examples

Input: S = "abc", shift[][] = {{0, 1}, {1, 2}}
Output: cab
Explanation: 
[0, 1] refers to shifting  S[0] to the left by 1. Therefore, the string S modifies from "abc" to "bca".
[1, 2] refers to shifting  S[0] to the right by 1. Therefore, the string S modifies from "bca"to "cab".

Input: S = "abcdefg", shift[][] = { {1, 1}, {1, 1}, {0, 2}, {1, 3} }
Output: efgabcd
Explanation:  
[1, 1] refers to shifting S[0] to the right by 1. Therefore, the string S modifies from "abcdefg" to "gabcdef".
[1, 1] refers to shifting S[0] to the right by 1. Therefore, the string S modifies from "gabcdef" to "fgabcde".
[0, 2] refers to shifting S[0] to the left by 2. Therefore, the string S modifies from "fgabcde" to "abcdefg".
[1, 3] refers to shifting S[0] to the right by 3. Therefore, the string S modifies from "abcdefg" to "efgabcd".

Naive Approach: The simplest approach to solve the problem is to traverse the matrix shift[][] and shift S[0] by amount number of indices in the specified direction. After completing all shift operations, print the final string obtained.
Time Complexity: O(N2)
Auxiliary space: O(N)

Efficient Approach: To optimize the above approach, follow the steps below:

  • Initialize a variable, say val, to store the effective shifts.
  • Traverse the matrix shift[][] and perform the following operations on every ith row:
  • If shift[i][0] = 0 (left shift), then decrease val by -shift[i][1].
  • Otherwise (left shift), increase val by shift[i][1].
  • Update val =  val % len (for further optimizing the effective shifts).
  • Initialize a string, result = "", to store the modified string.
  • Now, check if val > 0. If found to be true, then perform the right rotation on the string by val.
  • Otherwise, perform left rotation of the string by |val| amount.
  • Print the result.

Below is the implementation of the above approach:


Output: 
cab

 


 

Time Complexity: O(N)
Auxiliary Space: O(N)

Please refer complete article on Modify a string by performing given shift operations for more details!

Comment
Article Tags: