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?
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
Today's daily challenge "Reverse words in a string" - Python Solution Nov 13, 2022
r/leetcode 3y ago
In my opinion this is the simplest and modern way to reverse a string. What do you think? Do you know easier ways? Feb 1, 2021
r/learnjavascript 5y ago
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
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
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
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
Videos
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
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.
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.
WalkCCC
walkccc.me › LeetCode › problems › 344
344. Reverse String - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
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.
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("]"); }