public Set<String> getAnagrams(String word) { //TODO n-grams Set<String> anagrams = new HashSet<String>(); char[] chars = word.toLowerCase().toCharArray(); List<Character> list = Arrays.asList(ArrayUtils.toObject(chars)); PermutationIterator<Character> gen = new PermutationIterator<Character>(list); StringBuilder sb = new StringBuilder(); while (gen.hasNext()) { List<Character> anagramList = gen.next(); for (Character ch : anagramList) { sb.append(ch); } String anagram = sb.toString(); if (Checker.formsDictionary.containsKey(anagram)) { anagrams.add(anagram); } sb.setLength(0); } anagrams.remove(word.toLowerCase()); // don't return the input word return anagrams; }
@Override public void startBenchmarkInternal() { LOG.info(String.format("Executing %s Benchmark . . . .", type.longname())); if (bench.permuteBenchmarks()) { PermutationIterator<GraphDatabaseType> iter = new PermutationIterator<GraphDatabaseType>( bench.getSelectedDatabases()); int cntPermutations = 1; while (iter.hasNext()) { LOG.info("Scenario " + cntPermutations); startBenchmarkInternalOnePermutation(iter.next(), cntPermutations); cntPermutations++; } } else { startBenchmarkInternalOnePermutation(bench.getSelectedDatabases(), 1); } LOG.info(String.format("%s Benchmark finished", type.longname())); post(); }
/** * Returns a {@link Collection} of all the permutations of the input collection. * <p> * NOTE: the number of permutations of a given collection is equal to n!, where * n is the size of the collection. Thus, the resulting collection will become * <b>very</b> large for collections > 10 (e.g. 10! = 3628800, 15! = 1307674368000). * <p> * For larger collections it is advised to use a {@link PermutationIterator} to * iterate over all permutations. * * @see PermutationIterator * * @param <E> the element type * @param collection the collection to create permutations for, may not be null * @return an unordered collection of all permutations of the input collection * @throws NullPointerException if collection is null * @since 4.0 */ public static <E> Collection<List<E>> permutations(final Collection<E> collection) { final PermutationIterator<E> it = new PermutationIterator<E>(collection); final Collection<List<E>> result = new LinkedList<List<E>>(); while (it.hasNext()) { result.add(it.next()); } return result; }