LeetCode
leetcode.com › problems › reverse-string-ii
Reverse String II - LeetCode
Reverse String II - Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If there are fewer than k characters left, reverse all of them.
LeetCode
leetcode.com › problems › reverse-words-in-a-string-ii
Reverse Words in a String II - LeetCode
Can you solve this real interview question? Reverse Words in a String II - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Videos
09:12
344. Reverse String | 541. Reverse String II && LEETCODE EASY - ...
09:40
Reverse String II | Leetcode 541 | Amazon Google Facebook interview ...
04:27
Reverse String - Leetcode 344 - Java - YouTube
04:37
Reverse String - Leetcode 344 - 2 Pointers - (Python) - YouTube
09:03
Reverse String - 3 Ways - Leetcode 344 - Python - YouTube
AlgoMonster
algo.monster › liteproblems › 541
541. Reverse String II - In-Depth Explanation
In-depth solution and explanation for LeetCode 541. Reverse String II in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
WalkCCC
walkccc.me › LeetCode › problems › 541
541. Reverse String II - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
LeetCode
leetcode.com › problems › reverse-words-in-a-string-iii
Reverse Words in a String III - LeetCode
Reverse Words in a String III - Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat ...
AlgoMonster
algo.monster › liteproblems › 186
186. Reverse Words in a String II - In-Depth Explanation
In-depth solution and explanation for LeetCode 186. Reverse Words in a String II in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
YouTube
youtube.com › watch
LeetCode 541 | Reverse String II | Algorithm Explained (Java) - YouTube
The description reads:"Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the str...
Published August 15, 2020
GitHub
github.com › doocs › leetcode › blob › main › solution › 0100-0199 › 0186.Reverse Words in a String II › README_EN.md
leetcode/solution/0100-0199/0186.Reverse Words in a String II/README_EN.md at main · doocs/leetcode
*/ function reverseWords(s: string[]): void { const n = s.length; const reverse = (i: number, j: number): void => { for (; i < j; ++i, --j) { [s[i], s[j]] = [s[j], s[i]]; } }; for (let i = 0, j = 0; j <= n; ++j) { if (s[j] === ' ') { reverse(i, j - 1); i = j + 1; } else if (j === n - 1) { reverse(i, j); } } reverse(0, n - 1); }
Author doocs
LeetCode
leetcode.com › problems › reverse-words-in-a-string
Reverse Words in a String - LeetCode
Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.
GitHub
github.com › doocs › leetcode › blob › main › solution › 0500-0599 › 0541.Reverse String II › README_EN.md
leetcode/solution/0500-0599/0541.Reverse String II/README_EN.md at main · doocs/leetcode
class Solution: def reverseStr(self, s: str, k: int) -> str: cs = list(s) for i in range(0, len(cs), 2 * k): cs[i : i + k] = reversed(cs[i : i + k]) return "".join(cs) class Solution { public String reverseStr(String s, int k) { char[] cs = s.toCharArray(); int n = cs.length; for (int i = 0; i < n; i += k * 2) { for (int l = i, r = Math.min(i + k - 1, n - 1); l < r; ++l, --r) { char t = cs[l]; cs[l] = cs[r]; cs[r] = t; } } return new String(cs); } }
Author doocs
LeetCode
leetcode.ca › all › 186.html
Leetcode 186. Reverse Words in a String II
Given an input string , reverse the string word by word.
Spark Code Hub
sparkcodehub.com › leetcode › 541 › reverse-string-ii
LeetCode 541: Reverse String II Solution in Python – A Step-by-Step Guide
k = 2, you’d flip "ab" to "ba" and "cd" to "dc," leaving "efg" as is, resulting in "bacdfeg." That’s the fun challenge of LeetCode 541: Reverse String II, an easy-to-medium problem that’s a fantastic way to practice string manipulation in Python.
AlgoMap
algomap.io › question-bank › reverse-words-in-a-string-ii
Reverse Words in a String II
The key to solving the "Reverse Words in a String II" problem in-place is to use the two-step reverse technique: first reverse the whole array to get the words in the correct order, then reverse each word to restore their original spelling.
AlgoMonster
algo.monster › liteproblems › 344
344. Reverse String - In-Depth Explanation
In-depth solution and explanation for LeetCode 344. Reverse String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
Reddit
reddit.com › r/learnpython › reversing a string leetcode question
r/learnpython on Reddit: reversing a string LeetCode question
May 9, 2023 -
So the code goes:
s = [“h”, ”e”, ”l”, ”l”, ”o”]
s[:] = s[::-1] print(s)
This prints the reverse of s in both VSCode and on the LeetCode website ide
I tried to see if the follow would work:
s = [“h”, ”e”, ”l”, ”l”, ”o”]
s = s[::-1] print(s)
This did work but only on VSCode not on leet code. So my question is why did it work in VSCode and not LeetCode?
There’s other solutions I tried to the problem that work in VSCode but not in LeetCode shock I find it odd, am I doing something I shouldn't?
Top answer 1 of 5
39
Does the leetcode question require you to reverse the string in place or to return a reversed version of the string? If it is the first one, that is why the second option doesn't work for you. Lists are passed by reference in Python as opposed to being passed by value. s[:] = s[::-1] #this modifies the memory space that s is pointing to. s = s[::-1] #this sets s to a new list in a new location in memory.
2 of 5
2
In addition to the other comments, I want to say that they want you to write code to reverse the string without using Python’s syntactic sugar or builtin string reverse functions. You have to use loops and all