> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Perform String Shifts Python Solution

> Tested Python solution for LeetCode 1427 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1427, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/perform-string-shifts/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1427   # by problem number
lcpy gen -s perform_string_shifts   # by problem name
```

## Problem

You are given a string `s` containing lowercase English letters, and a matrix `shift`, where `shift[i] = [direction<sub>i</sub>, amount<sub>i</sub>]`:

* `direction<sub>i</sub>` can be `0` (for left shift) or `1` (for right shift).
* `amount<sub>i</sub>` is the amount by which string `s` is to be shifted.
* A left shift by 1 means remove the first character of `s` and append it to the end.
* Similarly, a right shift by 1 means remove the last character of `s` and add it to the beginning.

Return the final string after all operations.

### Examples

```
Input: s = "abc", shift = [[0,1],[1,2]]
Output: "cab"
Explanation:
[0,1] means shift to left by 1. "abc" -> "bca"
[1,2] means shift to right by 2. "bca" -> "cab"
```

```
Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]
Output: "efgabcd"
Explanation:
[1,1] means shift to right by 1. "abcdefg" -> "gabcdef"
[1,1] means shift to right by 1. "gabcdef" -> "fgabcde"
[0,2] means shift to left by 2. "fgabcde" -> "abcdefg"
[1,3] means shift to right by 3. "abcdefg" -> "efgabcd"
```

### Constraints

* `1 <= s.length <= 100`
* `s` only contains lower case English letters.
* `1 <= shift.length <= 100`
* `shift[i].length == 2`
* `direction<sub>i</sub>` is either `0` or `1`.
* `0 <= amount<sub>i</sub> <= 100`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/perform_string_shifts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/perform_string_shifts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) where n = len(s), m = len(shift)
    # Space: O(n) for the result string
    def string_shift(self, s: str, shift: list[list[int]]) -> str:
        offset = sum(amount if direction == 1 else -amount for direction, amount in shift)
        offset %= len(s)
        split = len(s) - offset
        return s[split:] + s[:split]
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where n = len(s), m = len(shift) | O(n) for the result string |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.