Skip to content

Instantly share code, notes, and snippets.

@rainiera
Last active August 29, 2015 14:16
Show Gist options
  • Save rainiera/155a08fd05c36eab537c to your computer and use it in GitHub Desktop.
Save rainiera/155a08fd05c36eab537c to your computer and use it in GitHub Desktop.
import java.util.HashSet;
import java.util.Random;
/*
* Given a string, write a function that returns the string, unaltered.
*/
public class StringString {
public static void main(String[] args) {
System.out.println(string("string"));
}
public static String string (String string) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char temp = string.charAt(i);
sb.append(temp);
}
String tempTempTemp = randomShuffle(sb.toString());
while (!tempTempTemp.equals(string)) {
String tempTemp = sb.toString();
tempTempTemp = randomShuffle(tempTemp);
}
return tempTempTemp;
}
public static String randomShuffle (String string) {
HashSet<Integer> set = new HashSet<Integer>();
char[] stringCharArray = string.toCharArray();
Random r = new Random();
StringBuffer sb = new StringBuffer();
int temp = 0;
while (temp < stringCharArray.length) {
int index = r.nextInt(string.length());
if (!set.contains(index)) {
set.add(index);
sb.append(stringCharArray[index]);
temp++;
}
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment