Google interview question

Implement string rotateString(string input, int amt)

Interview Answers

Anonymous

9 Mar 2015

one other option is storing input in a circular linked list and then moving the head pointer by "amt" places

8

Anonymous

12 Mar 2015

Another option is to use substring. You can invert the substring from 0 to length-modulo rotation with the substrin length-modulo rotation to the end. This is an example: for string abcd dabc (1 char rotation) cdab (2 chars rotation) bcda (3 chars rotation) abcd (4 chars rotation) dabc (5 chars rotation) This is an implementation in Java: public static String rotateString(String str, int rot) { int mod = rot % str.length(); return str.substring(str.length() - mod, str.length()) + str.substring(0, str.length() - mod); } I like the idea of circular linked list, though.

3

Anonymous

13 Apr 2015

Here's a java solution like an above answer, but also accepts negative amounts for bidirectional rotation: public String rotate(String s, int rot) { int len = s.length(); rot = rot % len; if (rot == 0) return s; if (rot > 0) { return s.substring(len - rot) + s.substring(0, len - rot); } else { return rotate(s, len - Math.abs(rot)); } }

1

Anonymous

17 May 2015

Faster way would be to use tmp additional memory O(n) and append the same string to the tail of original string. For example if "ABCD" is your string and rotate by 2. Then create "ABCDABCD" as the new string and move the head start index to 2. Copy from 2 to n into your output string.

1

Anonymous

7 Mar 2015

Create output string as same length of input, declare inputIndex and outputIndex variables, set inputIndex to (amt % input.length) and outputIndex to 0. Loop through, copying characters from input[inputIndex] to output[outputIndex]. Increment both inputIndex and outputIndex at each iteration. When inputIndex == input.length-1, set inputIndex to 0 and continue copying to output. When outputIndex == input.length-1, stop loop and return output.

Anonymous

28 Mar 2015

void RotateInStr(char* str, int amt, int iStrLen) { int iFromPos = 0; int iTargetPos = amt; while (true) { for (int i=iFromPos; i < amt; ++i) { int iToPos = (iTargetPos + i)%iStrLen; char tmp = str[iToPos]; str[iToPos] = str[i]; str[i] = tmp; } iTargetPos += amt; if (iStrLen < iTargetPos) { iTargetPos -= iStrLen; if (amt == 2*iTargetPos) ; else if (2*iTargetPos < amt) { RotateInStr(str+iTargetPos, iTargetPos, amt - iTargetPos); } else { int iBuf = amt - iTargetPos; int iRemain = iTargetPos % iBuf; if (iRemain == 0) ; else RotateInStr(str+iTargetPos, iRemain, iBuf); } break; } else if (iStrLen == iTargetPos) break; } } void Rotate(char* str, int amt) { if (!amt) return; int iStrLen = (int)strlen(str); amt = amt % iStrLen; RotateInStr(str, amt, iStrLen); }

Anonymous

10 Apr 2015

public class StringRotate { public static String rotate(String ip,int n) { char[] ipArray=ip.toCharArray(); reverse(ipArray,0,n-1); reverse(ipArray, n, ip.length()-1); reverse(ipArray, 0,ip.length()-1); ip=String.valueOf(ipArray); return ip; } public static void reverse(char[] ip,int low,int high) { while(low