- Notifications
You must be signed in to change notification settings - Fork2.4k
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 intomainChoose a base branch fromohtani
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
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
ranveersingh27185540f79 fix article
ranveersingh27188c6c43a fix csharp method signature
ranveersingh2718a7b453d fix wiggle-sort python heap solution
ranveersingh27187fdfc86 Merge branch 'main' of https://github.com/neetcode-gh/leetcode into o…
ranveersingh2718de5bc67 add confusing-number.md article
ranveersingh2718b2cce1b Merge branch 'ohtani' of https://github.com/neetcode-gh/leetcode into…
ranveersingh271868ca978 Update explanation of L in complexity analysis
neetcode-gh6e89d08 Fix logarithm notation in complexity section
neetcode-ghc0ba543 Fix LaTeX formatting for logarithm in documentation
neetcode-gh1699312 add perform-string-shifts article
ranveersingh2718ffb7f76 Merge branch 'main' of https://github.com/neetcode-gh/leetcode into o…
ranveersingh27183dcb251 Merge branch 'ohtani' of https://github.com/neetcode-gh/leetcode into…
ranveersingh2718File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletionsarticles/perform-string-shifts.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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 |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.