Anonymous user
Best shuffle: Difference between revisions
→{{header|Java}}: fixed bug, added optimization
(→{{header|D}}: small optimization) |
(→{{header|Java}}: fixed bug, added optimization) |
||
Line 872:
=={{header|Java}}==
Translation of [[Best_shuffle#Icon_and_Unicon|Icon]] via [[Best_shuffle#AWK|AWK]]
<lang java>import java.util.
public class BestShuffle {
public static void main(String[] args) {
String[] words = {"abracadabra", "seesaw", "grrrrrr", "pop", "up", "a"};
for (String w : words)
System.out.println(bestShuffle(w));
}
Line 887 ⟶ 885:
Collections.shuffle(Arrays.asList(s2));
int i = 0, j = 0;
for (
for (int i
if (s2[i] != s2[j] && s2[i] != s1.charAt(j) && s2[j] != s1.charAt(i)) {
char tmp = s2[i];
s2[i] = s2[j];
Line 897 ⟶ 896:
j++;
}
i++
j = 0;
}
return s1 + " " + new String(s2) + " (" + count(s1, s2) + ")";
|