Created
August 2, 2018 17:26
-
-
Save RameshRM/be367f0a79ba90588e033348afc3225b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rotator { | |
public String rotate(String input, int maxRotations) { | |
int current = -1; | |
char[] letters = input.toCharArray(); | |
final String original = input; | |
int start = input.length() - maxRotations; | |
// | |
for (int i = 0; i < letters.length; i++) { | |
input = rotateRight(input); | |
String left = input.substring(0, (i + 1)); | |
String right = input.substring((i + 1)); | |
System.out.println(String.format("%s-%s%s", original, right, left)); | |
System.out.println(String.format("%s:%s:%s-%s", i, input, left, right)); | |
System.out.println(input.substring(input.length() - 1)); | |
// current++; | |
// char tmp = input.charAt(i); | |
// char tmp2 = input.charAt(current); | |
// | |
// letters[i] = tmp2; | |
// letters[current] = tmp; | |
} | |
// return rotateRight(input); | |
return new String(letters); | |
} | |
private String rotateRight(String input) { | |
return String.format("%s%s", input.substring(input.length() - 1), input.substring(0, input.length() - 1)); | |
} | |
public static void main(String[] args) { | |
Rotator rotator = new Rotator(); | |
String stringOne = rotator.rotate("tokyo", 2); | |
System.out.println(stringOne); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment