Reverse String in O(N/2) with Iteration
        /**
        Problem Statement: Give an array of characters which is non empty 
        
        Input 1: ["h"]
        Output1: ["h"]
        
        Input 2: ["h", "i"]
        Output2: ["i", "h"]

        Input 2: ["h", "e", "y"]
        Output2: ["y", "e", "h"]

        Observation: If the array size is even then half the iteration we can swap in place 
                     If the array size is odd then half -1 times iteration for swaping. 
        Time O(N/2)
        Space O(N+1) = O(N)
        
       */
        if(s.length > 1) {
            char temp;
            int length = s.length -1;
            Double iteration = Math.floor(s.length/ 2);
            for(int i = 0 ; i < iteration.intValue(); i++) {
                temp = s[i];
                s[i] = s[length];
                s[length] = temp;
                --length;
            }
           
        }
    }
	```
Comments (0)