🌐
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.
🌐
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?

Discussions

reversing a string LeetCode question
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. More on reddit.com
🌐 r/learnpython
12
36
May 9, 2023
Reverse words in a string using multithreading?
UPDATE: Since the question was clarified. I'll have to change my answer slightly. Input: "The cat is a dog" Output: "dog a is cat The" One trick to solve this problem is to reverse the entire string once and then reverse the words. So something like this: "The cat is a dog" => "god a si tac ehT" => "dog a is cat The" So you could parallelize the first step. That would require you to choose smaller chunks of ranges within the larger range of 0 to n/2 And then you could parallelize the second step, but only after the first step is completely done. And the second step would look like what I was describing earlier: A helper function called reverseWords(str, start int, end int) This would allow you to process the str chunk by chunk independently on different threads. You would just need to ensure that the cut off points only land on spaces (so as not to cut a word in half). Then, it just becomes a two pointer problem, or you could use a stack if you prefer. More on reddit.com
🌐 r/leetcode
12
4
April 23, 2024
After 3 hours of pain and misery, my solution to “String to Integer” on LeetCode
Your approach to taking screenshots is deeply disturbing. More on reddit.com
🌐 r/programminghorror
169
2245
January 4, 2024
I just lost a really good job opportunity because I couldn't reverse a linked list.
It's okay I just bombed an interview by writing my code in my IDE then missing the submission timer by like a second and submitted an empty answer. More on reddit.com
🌐 r/cscareerquestions
380
959
July 2, 2020
🌐
AlgoMonster
algo.monster › liteproblems › 344
344. Reverse String - In-Depth Explanation
The solution uses a two-pointer technique where one pointer i starts at the beginning of the array (index 0) and another pointer j starts at the end (index len(s) - 1). The algorithm repeatedly swaps the characters at positions i and j, then moves i forward and j backward.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0300-0399 › 0344.Reverse String › README_EN.md
leetcode/solution/0300-0399/0344.Reverse String/README_EN.md at main · doocs/leetcode
The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. ... $j$, initially pointing to the start and end of the array respectively.
Author   doocs
🌐
AlgoMap
algomap.io › problems › reverse-string
344. Reverse String - Leetcode Solution
The optimal way to reverse a string in-place is by using the two-pointer technique.
🌐
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.
🌐
Medium
medium.com › @saikrishna.a.dev › leetcode-344-reverse-string-eae77e00c91c
LeetCode 344 : Reverse String. How to reverse a string in place ? | by Saikrishna A | Medium
November 2, 2024 - The two-pointer approach provides a straightforward and efficient solution to reversing a string in place. This method maintains O(n) time complexity with minimal space overhead, making it ideal for handling large strings. Let me know if you’d like to add any further details or examples! Here is the link to the problem https://leetcode.com/problems/reverse-string/.
🌐
Medium
medium.com › repeat-code-with-leetcode › repeat-code-with-leetcode-reverse-a-string-a4c647259d8a
Repeat Code With LeetCode — Reverse A String | by Evan SooHoo | Repeat Code With LeetCode | Medium
May 2, 2024 - The idea here is that you set one pointer to the beginning of the string, one pointer to the end, and then swap characters until you hit the middle. That’s it. I have taken a look at other Two-Pointer problems, and most of them are quite a ...
Find elsewhere
🌐
LeetCode
leetcode.com › problems › reverse-string-ii › description
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.
🌐
Paul Coroneos
pcoroneos.com › blog › leetcode › 344-reverse-string
Leetcode 344 - Reverse String - Paul Coroneos
We are asked to do this in place. This means we cannot create a new array to store the reversed string. Temporary variables are allowed.
🌐
NeetCode
neetcode.io › solutions › 344. reverse string
LeetCode 344 Reverse String Solution & Explanation | NeetCode
You are given an array of characters which represents a string s. Write a function which reverses a string. You must do this by modifying the input array…
🌐
WalkCCC
walkccc.me › LeetCode › problems › 344
344. Reverse String - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
🌐
DEV Community
dev.to › ranggakd › reverse-string-leetcode-python-418m
Reverse String | LeetCode | Python - DEV Community
May 17, 2023 - class Solution: def reverseString(self, s: List[str]) -> None: l = len(s) for i in range(l//2): if s[i] != s[l-1-i]: s[i], s[l-1-i] = s[l-1-i], s[i]
🌐
Medium
medium.com › @mushkansingh733 › leetcode-344-reverse-string-909fc1ade07c
Leetcode 344 : Reverse String. 🔹 Problem Statement | by Mushkansingh | Medium
March 5, 2025 - Swap the first and last character, second and second-last, and so on until the middle. function reverseString(s) { let arr = s.split(''); // Convert string to array let left = 0, right = arr.length - 1; while (left < right) { // Swap characters ...
🌐
PROGIEZ
progiez.com › 344-reverse-string-leetcode-solution
344. Reverse String LeetCode Solution - Progiez
February 17, 2025 - class Solution: def reverseString(self, s: list[str]) -> None: l = 0 r = len(s) - 1 while l < r: s[l], s[r] = s[r], s[l] l += 1 r -= 1 # code by PROGIEZ · Explore all LeetCode problem solutions at Progiez here ... Happy Coding! Keep following PROGIEZ for more updates and solutions. 3563. Lexicographically Smallest String After Adjacent Removals LeetCode Solution
🌐
Medium
lydiaplu.medium.com › leetcode-541-reverse-string-ii-24a879b40fdd
LeetCode — 541. Reverse String II | by Pan Lu | Medium
January 31, 2024 - String manipulation is a common task in programming. In this blog post, we explore a solution for a specific string manipulation challenge: reversing every first ‘k’ character for every ‘2k’ character in a string.
🌐
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.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0100-0199 › 0151.Reverse Words in a String › README_EN.md
leetcode/solution/0100-0199/0151.Reverse Words in a String/README_EN.md at main · doocs/leetcode
Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space? ... $j$ to find each word, add it to the result list, then reverse the result list, and finally concatenate it into a string.
Author   doocs
🌐
LeetCode
leetcode.com › problems › reverse-degree-of-a-string
Reverse Degree of a String - LeetCode
The reverse degree is calculated as follows: 1. For each character, multiply its position in the reversed alphabet ('a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-indexed).