🌐
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

java - 344. Reverse String LeetCode - Stack Overflow
I've got this code that runs in my IDE, Intellij but it will not run in LeetCode. Am I misunderstanding the question? Write a function that reverses a string. The input string is given as an array... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
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.
🌐
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/.
🌐
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
🌐
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 ...
🌐
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.
Find elsewhere
🌐
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…
🌐
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.
🌐
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]
🌐
LeetCode
leetcode.com › problems › reverse-string-prefix
Reverse String Prefix - LeetCode
Can you solve this real interview question? Reverse String Prefix - You are given a string s and an integer k. Reverse the first k characters of s and return the resulting string.
🌐
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.
🌐
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
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 66214162 › 344-reverse-string-leetcode
java - 344. Reverse String LeetCode - Stack Overflow
The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the characters consist of printable ascii characters. class Solution { public void reverseString(char[] s) { int arrayLength = s.length - 1; System.out.print("["); for (int i = 0; i < s.length; i++) { int temp = s.length - i - 1; if (i != arrayLength) { System.out.printf("\"%s\",", s[temp]); } else { System.out.printf("\"%s\"", s[temp]); } } System.out.print("]"); }