Reverse String — Solution in Python, Java & C++
Reverse String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Reverse String is an easy coding problem on strings and two pointers, asked in Amazon, Microsoft, TCS and Infosys interviews.
- Difficulty: Easy
- Topics: Strings, Two Pointers
- Asked at: Amazon, Microsoft, TCS, Infosys, Apple, HCL, Mphasis, Virtusa
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Write a function that reverses a string and returns the result.
The classic form of this question hands you a character array and asks you to reverse it in place with O(1) extra memory; think in those terms even though this version returns a new string.
Example 1
Input: s = "hello"
Output: olleh
Example 2
Input: s = "Hannah"
Output: hannaH
Example 3
Input: s = "a"
Output: a
Constraints
1 <= s.length <= 40s consists of printable ASCII letters.
Hints for Reverse String
Hint 1
Two pointers, one at each end, swapping and stepping inward.
Hint 2
Stop when they meet; a middle character in an odd-length string swaps with itself and needs no special case.
How to solve Reverse String
Two pointers, one at each end, swap and step inward until they meet.
Approach
- Copy the string into a mutable character array.
- With
left = 0andright = n - 1, swap the two characters and move both pointers inward. - Stop when
left >= rightand join the array.
Why it works
Each swap places two characters in their final mirrored positions; after ⌊n/2⌋ swaps every character has been exchanged with its mirror. A middle character in an odd-length string is already in place.
Complexity
- Time —
O(n) - Space —
O(n) for the copy — O(1) extra in the in-place array form
Pitfalls
- Using
<=as the loop condition swaps the middle character with itself — harmless, just wasteful. - The classic form mutates an array; a language-level reverse is fine here but is not the technique being tested.
Reference solution
Python
def reverseString(s: str) -> str:
chars = list(s)
left, right = 0, len(chars) - 1
while left < right:
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
return "".join(chars)Java
public static String reverseString(String s) {
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
char t = chars[left];
chars[left] = chars[right];
chars[right] = t;
left++;
right--;
}
return new String(chars);
}C++
string reverseString(string s) {
int left = 0, right = (int) s.size() - 1;
while (left < right) {
char t = s[left];
s[left] = s[right];
s[right] = t;
left++;
right--;
}
return s;
}JavaScript
var reverseString = function(s) {
const chars = s.split("");
let left = 0, right = chars.length - 1;
while (left < right) {
const t = chars[left];
chars[left] = chars[right];
chars[right] = t;
left++;
right--;
}
return chars.join("");
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Two Pointers Technique