← Home

Perform String Shifts

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
LC

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For Perform String Shifts, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are stringShift.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01class Solution {
02public:
03    string stringShift(string s, vector<vector<int>>& shift) {
04        int shiftRightVal = 0;
05        for(vector<int>& p : shift){
06            shiftRightVal += p[1] * ((p[0] == 0) ? -1 : 1);
07        }
08        
09        if(shiftRightVal == 0){
10            return s;
11        }
12        
13        string ans = "";
14        
15        if(shiftRightVal > 0){
16            shiftRightVal = shiftRightVal % s.size();
17            ans = s.substr(s.size()-shiftRightVal) + s.substr(0, s.size()-shiftRightVal);
18        }else{
19            int shiftLeftVal = -shiftRightVal;
20            shiftLeftVal = shiftLeftVal % s.size();
21            ans = s.substr(shiftLeftVal) + s.substr(0, shiftLeftVal);
22        }
23        
24        return ans;
25    }
26};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.