You are given a string and need to return a new string with the characters in reverse order.
Nothing is removed or changed along the way. The same characters should still be present, but their order should run from the last character back to the first. If the input is empty, the answer is also empty. If the input has only one character, it stays the same after reversing.
For example, s = "abc" should return "cba", and s = "hello" should return "olleh".
So the task is simply to flip the character order of the input string and return the reversed result.
Example Input & Output
The string 'hello' reversed.
Characters in 'abc' reversed.
Reversing an empty string.
Algorithm Flow

Solution Approach
A very natural Java solution for this problem is to use StringBuilder. Strings in Java are immutable, which means you do not modify the original string in place. Instead, you create a helper object that can build the reversed result efficiently and then convert it back into a string.
The shortest clean implementation is:
return new StringBuilder(text).reverse().toString();
This works in three steps. First, new StringBuilder(text) creates a mutable builder containing the original characters. Second, reverse() flips the character order inside that builder. Third, toString() turns the builder back into the final string result.
This is a strong choice because it is readable and uses a standard Java library method designed exactly for this job. You could also solve the problem manually by looping backward through the string and appending characters one by one, but StringBuilder.reverse() is simpler and communicates the intent immediately.
The runtime is O(n) because every character needs to be visited as part of the reversal, and the extra space is also O(n) for the reversed result. For a basic Java string-reversal task, this is the most straightforward and idiomatic solution.
Best Answers
public class Solution {
public String reverseString(String text) {
if (text == null) return null;
return new StringBuilder(text).reverse().toString();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
