Simple go solution


  • 0
    G

    Approach #1: Iterate over the string

    This is a simple solution in go. First, allocate a rune array of equal length to hold the result. Then iterate over each character of the string and set that into the array. Return the array as a string.

    func reverseString(s string) string {
        l := len(s);
        result := make([]rune, l);
        for i, n := range s {
            result[l-i-1] = n;
        }
        return string(result);
    }
    

    Complexity Analysis
    Time Complexity : O(s.Length) as we are iterating over the string s.
    Space Complexity : O(s.Length) as we allocate rune buffer of same size to hold the result.


Log in to reply
 

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