@Test public void testAnagrams() { List<String> dictionary = new ArrayList<>(); dictionary.add("urn"); dictionary.add("won"); dictionary.add("bear"); dictionary.add("bare"); dictionary.add("run"); dictionary.add("cafe"); dictionary.add("now"); List<List<String>> anagrams = anagrams(dictionary); Assert.assertEquals(3, anagrams.size()); Assert.assertTrue(anagrams.contains(Arrays.asList(new String[] { "urn", "run" }))); Assert.assertTrue(anagrams.contains(Arrays.asList(new String[] { "won", "now" }))); Assert.assertTrue(anagrams.contains(Arrays.asList(new String[] { "bear", "bare" }))); }
@SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr); hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; }
PracticeQuestionsCh12 { @SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr); hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; } }
PracticeQuestionsCh12 { @SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr); hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; }  }
PracticeQuestionsCh12 { @SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr); hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; }  @SuppressWarnings("unchecked") static List<List<String>> anagrams(final List<String> dictionary); }
PracticeQuestionsCh12 { @SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr); hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; }  @SuppressWarnings("unchecked") static List<List<String>> anagrams(final List<String> dictionary); static final Logger LOGGER; }