/** Calculates the number of permutations for 5 elements */ @Test public void testPermutations2() { permutations = new Permutations(5); permutations.backtracking(0); int result = permutations.getNumberOfPermutations(); assertEquals(120, result); }
/** Gets all permutations of a given word. */ public ArrayList<String> getPermutations() { ArrayList<String> result = new ArrayList<String>(); // The empty string has a single permutation: itself if (word.length() == 0) { result.add(word); return result; } // Loop through all character positions for (int i = 0; i < word.length(); i++) { // Form a simpler word by removing the ith character String shorterWord = word.substring(0, i) + word.substring(i + 1); // Generate all permutations of the simpler word Permutations shorterPermutations = new Permutations(shorterWord); ArrayList<String> shorterWordPermutations = shorterPermutations.getPermutations(); // Add the removed character to the front of // each permutation of the simpler word, for (String s : shorterWordPermutations) { result.add(word.charAt(i) + s); } } // Return all permutations return result; }
@Test public void testStringsSameLength() { assertEquals(Permutations.arePermuations("hellman", "lehlnam"), true); assertEquals( Permutations.arePermuations("Lorem Ipsum Dolor Sit Amet", "Ipsum Dolor Amet Sit Lorem"), true); }
@Test public void testSimple() throws Exception { final String s = "ab"; final Set<String> expected = new HashSet<>(Arrays.asList("ab", "ba")); assertEquals(expected, Permutations.perms(s)); }
@Test public void testDifferentLengthsAreNot() { assertEquals(Permutations.arePermuations("asd", "asd "), false); assertEquals(Permutations.arePermuations("asdasdasd", ""), false); assertEquals(Permutations.arePermuations("wdwdwdw", "wxwxwxw"), false); }
@Test public void testSingletons() { assertEquals(Permutations.arePermuations("a", "b"), false); assertEquals(Permutations.arePermuations("a", "a"), true); }
@Test public void testEmptyStringsArePermutations() { String arg1 = ""; String arg2 = ""; assertEquals(Permutations.arePermuations(arg1, arg2), true); }