599. Perform String Shifts

You are given a string s containing lowercase English letters and a matrix shift, where shift[i] = [directionᵢ, amountᵢ]:

  • directionᵢ can be 0 (for left shift) or 1 (for right shift).
  • amountᵢ is the number of positions by which the string s should be shifted.
  • A left shift by 1 means removing the first character of s and appending it to the end.
  • A right shift by 1 means removing the last character of s and adding it to the beginning.

Return the final string after applying all shift operations.

Example 1:

Input: s = "xyz", shift = [[1,2],[0,1]]

Output: "zxy"  

Explanation:

  • [1,2] means shift right by 2: "xyz" → "yzx"
  • [0,1] means shift left by 1: "yzx" → "zxy"

Example 2:

Input: s = "hello", shift = [[0,3],[1,2]]

Output: "elloh"  

Explanation:

  • [0,3] means shift left by 3: "hello" → "lohel"
  • [1,2] means shift right by 2: "lohel" → "elloh"

Now Your Turn!

Pick the correct output for the given input

Input: s = "abcdef", shift = [[1,1],[0,2],[1,3],[0,1]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 100
  • s consists only of lowercase English letters.
  • 1 <= shift.length <= 100
  • shift[i].length == 2
  • directionᵢ is either 0 or 1.
  • 0 <= amountᵢ <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string stringShift(string s, vector<vector<int>>& shift) {
// Your code goes here
}
};
Test Case

Input:

S
Shift