8. Rotate String
Description
Given a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
Solution
public class Solution {
/**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
if(str == null || str.length==0) return;
offset = offset % str.length;
String s = String.copyValueOf(str);
String ss = s.substring(s.length()-offset) + s.substring(0,s.length()-offset);
for(int i=0;i<ss.length();i++){
str[i] = ss.charAt(i);
}
}
}