Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add perform-string-shifts article#4977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ranveersingh2718 wants to merge13 commits intomain
base:main
Choose a base branch
Loading
fromohtani
Open
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
8df8b99
add maximum-distance-in-arrays article
ranveersingh2718Nov 3, 2025
5540f79
fix article
ranveersingh2718Nov 3, 2025
8c6c43a
fix csharp method signature
ranveersingh2718Nov 3, 2025
a7b453d
fix wiggle-sort python heap solution
ranveersingh2718Nov 4, 2025
7fdfc86
Merge branch 'main' of https://github.com/neetcode-gh/leetcode into o…
ranveersingh2718Nov 7, 2025
de5bc67
add confusing-number.md article
ranveersingh2718Nov 7, 2025
b2cce1b
Merge branch 'ohtani' of https://github.com/neetcode-gh/leetcode into…
ranveersingh2718Nov 7, 2025
68ca978
Update explanation of L in complexity analysis
neetcode-ghNov 7, 2025
6e89d08
Fix logarithm notation in complexity section
neetcode-ghNov 7, 2025
c0ba543
Fix LaTeX formatting for logarithm in documentation
neetcode-ghNov 7, 2025
1699312
add perform-string-shifts article
ranveersingh2718Nov 9, 2025
ffb7f76
Merge branch 'main' of https://github.com/neetcode-gh/leetcode into o…
ranveersingh2718Nov 9, 2025
3dcb251
Merge branch 'ohtani' of https://github.com/neetcode-gh/leetcode into…
ranveersingh2718Nov 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 203 additions & 0 deletionsarticles/perform-string-shifts.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
## 1. Simulation

::tabs-start

```python
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
for direction, amount in shift:
amount %= len(s)
if direction == 0:
# Move necessary amount of characters from start to end
s = s[amount:] + s[:amount]
else:
# Move necessary amount of characters from end to start
s = s[-amount:] + s[:-amount]
return s
```

```java
class Solution {
public String stringShift(String s, int[][] shift) {
int len = s.length();

for (int[] move : shift) {
int direction = move[0];
int amount = move[1] % len;
if (direction == 0) {
// Move necessary amount of characters from front to end
s = s.substring(amount) + s.substring(0, amount);
} else {
// Move necessary amount of characters from end to front
s =
s.substring(len - amount) +
s.substring(0, len - amount);
}
}
return s;
}
}
```

```cpp
class Solution {
public:
string stringShift(string s, vector<vector<int>>& shift) {
int len = s.length();

for (auto move : shift) {
int direction = move[0];
int amount = move[1] % len;
if (direction == 0) {
// Move necessary amount of characters from start to end
s = s.substr(amount) + s.substr(0, amount);
} else {
// Move necessary amount of characters from end to start
s = s.substr(len - amount) +
s.substr(0, len - amount);
}
}
return s;
}
};
```

```javascript
class Solution {
/**
* @param {string} s
* @param {number[][]} shift
* @return {string}
*/
stringShift(s, shift) {
const len = s.length;

for (const move of shift) {
const direction = move[0];
const amount = move[1] % len;

if (direction === 0) {
// Move necessary amount of characters from front to end
s = s.substring(amount) + s.substring(0, amount);
} else {
// Move necessary amount of characters from end to front
s =
s.substring(len - amount) +
s.substring(0, len - amount);
}
}

return s;
}
}
```

::tabs-end

### Time & Space Complexity

- Time complexity: $O(N * L)$
- Space complexity: $O(L)$ extra space used

> Where $L$ is the length of the string and $N$ is the length of the `shift` array

---

## 2. Compute Net Shift

::tabs-start

```python
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
# Count the number of left shifts. A right shift is a negative left shift.
left_shifts = 0
for direction, amount in shift:
if direction == 1:
amount = -amount
left_shifts += amount

# Convert back to a positive, do left shifts, and return.
left_shifts %= len(s)
s = s[left_shifts:] + s[:left_shifts]
return s
```

```java
class Solution {
public String stringShift(String s, int[][] shift) {
// Count the number of left shifts. A right shift is a negative left shift.
int leftShifts = 0;

for (int[] move : shift) {
if (move[0] == 1) {
move[1] = -move[1];
}
leftShifts += move[1];
}

// Convert back to a positive, do left shifts, and return.
leftShifts = Math.floorMod(leftShifts, s.length());
s = s.substring(leftShifts) + s.substring(0, leftShifts);
return s;
}
}
```

```cpp
class Solution {
public:
string stringShift(string s, vector<vector<int>>& shift) {
// Count the number of left shifts. A right shift is a negative left shift.
int leftShifts = 0;

for (auto& move : shift) {
if (move[0] == 1) {
move[1] = -move[1];
}
leftShifts += move[1];
}

// Convert back to a positive, do left shifts, and return.
int n = s.length();
leftShifts = ((leftShifts % n) + n) % n;
s = s.substr(leftShifts) + s.substr(0, leftShifts);
return s;
}
};
```

```javascript
class Solution {
/**
* @param {string} s
* @param {number[][]} shift
* @return {string}
*/
stringShift(s, shift) {
// Count the number of left shifts. A right shift is a negative left shift.
let leftShifts = 0;

for (let move of shift) {
if (move[0] === 1) {
move[1] = -move[1];
}
leftShifts += move[1];
}

// Convert back to a positive, do left shifts, and return.
leftShifts = ((leftShifts % s.length) + s.length) % s.length;
s = s.substring(leftShifts) + s.substring(0, leftShifts);
return s;
}
}
```

::tabs-end

### Time & Space Complexity

- Time complexity: $O(N + L)$
- Space complexity: $O(L)$ extra space used

> Where $L$ is the length of the string and $N$ is the length of the `shift` array

[8]ページ先頭

©2009-2025 Movatter.jp