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.Arrays*;
import java.util.Collections;
 
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 (charint c1i := 0; i < s2.length; i++) {
forif (chars2[i] c2== : s2s1.charAt(i)) {continue;
for (int i if (c1 != c20; &&i c1< != s1s2.charAt(j)length; && c2 != s1.charAt(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;
j = 0;
}
return s1 + " " + new String(s2) + " (" + count(s1, s2) + ")";
Anonymous user