Code Logo

Reverse String

Published atDate not available
Easy 0 views
Like0

You can think of this as a small game with a very specific goal. In Reverse String, you are trying to work toward the right answer by following one clear idea.

Reverse string characters in place A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.

For example, if the input is s = "hello", the answer is "olleh". Example 1: Reverse "hello" to "olleh" Another example is s = "world", which gives "dlrow". Example 2: Reverse "world" to "dlrow"

This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.

One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.

Example Input & Output

Example 1
Input
s = "hello"
Output
"olleh"
Explanation

Example 1: Reverse "hello" to "olleh"

Example 2
Input
s = "world"
Output
"dlrow"
Explanation

Example 2: Reverse "world" to "dlrow"

Example 3
Input
s = "abc"
Output
"cba"
Explanation

Example 3: Reverse "abc" to "cba"

Algorithm Flow

Recommendation Algorithm Flow for Reverse String
Recommendation Algorithm Flow for Reverse String

Best Answers

java
class Solution {
    public String reverse_string(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}