LT 8 rotate string

Problem:

Given a char[] and an offset, rotate the char[] from the place of offset

example: abcdefg, and offset = 3, the output is efgabcd

Solution:

public String rotateString(String str, int offset){
    if(str == null || str.length() == 0) return null
    char[] chars = str.toCharArray();
    offset = offset % chars.length;

    reverseString(chars, 0, chars.length-1);
    reverse(chars, 0, offset-1);
    reverse(chars, offset, chars.length-1);
    return chars;
}

Last updated

Was this helpful?