Java very simple and clean solution


  • 0
    M

    This solution runs in O(n) as we have to go through the whole string from end to the begin and populate the reversed array.

    public class Solution {
        
        public String reverseString(String s) {
        
            char[] result = new char[s.length()];
            int pos = 0;
            for (int i = s.length() -1; i >= 0; i--) {
                result[pos++] = s.charAt(i);
            } 
            return new String(result);
    
        }
        
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.