Reverse String Leetcode Solution

Hello coders, In this blog, ” Reverse String Leetcode Solution “. We will see how to solve this problem using Java language. You must have seen problems similar to this, You just need to write a function that reverses the string. Let’s understand the problem statement in detail.

Problem statement:

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 with O(1) extra memory.

Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Example 2:

Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

Constraints:

Reverse String Leetcode Solution java

//Reverse String Leetcode Solution java
class Solution {
    public static void reverse(char[]s,int i,int j){
        if(i>=j){
            return;
        }
        
            
            char temp;
    temp = s[j];
    s[j] = s[i];
    s[i] = temp;
            reverse(s,++i,--j);
            
        
        
    }
    
    public void reverseString(char[] s) {
        int k=0;
        int l=s.length-1;
        reverse(s,k,l);
    }
    
    
}
//Reverse String Leetcode Solution java

Disclaimer: This problem(Reverse String Leetcode solution) is originally created by Leetcode. Codesagar only provides a solution for it. This solution is only for Educational and learning purposes.

Leave a Comment