Monday, June 29, 2015

Rotate String

Given a string and an offset, rotate string by offset. (rotate from left to right)
Have you met this question in a real interview? 
Yes
Example
Given "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"

Challenge
Rotate in-place with O(1) extra memory.

public class Solution {
    /*
     * param A: A string
     * param offset: Rotate string with offset.
     * return: Rotated string.
     */
    public char[] rotateString(char[] A, int offset) {
        // wirte your code here
        if(A == null || A.length == 0 || offset == 0) return A;
        offset = offset % A.length;
        reverse(A, A.length - offset, A.length-1);
        reverse(A, 0, A.length - offset - 1);
        reverse(A, 0, A.length-1);
        return A;
    }
    private void reverse(char[] A, int i, int j){
        while(i < j){
            char temp = A[i];
            A[i] = A[j];
            A[j] = temp;
            i++; j--;
        }
    }
};

No comments:

Post a Comment