Parth's Blog

© 2021. Parth Patil All rights reserved.

Group Anagram puzzle in various programming languages

Java solution

// Minus the boilerplate code
String[] words = {"abc", "bca", "mkzp", "cba"};

HashMap<String, ArrayList<String>> groups = new HashMap<String, ArrayList<String>>();

for (String w: words) {
  char[] chars = w.toCharArray();
  Arrays.sort(chars);
  String normalized = new String(chars);
  if (!groups.containsKey(normalized))
    groups.put(normalized, new ArrayList<String>());

  groups.get(normalized).add(w);
}

Scala solution

// Minus the boilerplate code
val words = Seq("abc", "bca", "mkzp", "cba")
words groupBy { word => word.toCharArray.sortWith(_ < _).mkString("") }

Coffeescript solution

  words = ["abc", "bca", "mkzp", "cba"]
  result = {}
  for word in words
    key = (word.split '').sort().join ''
    result[key] = [] if not result[key]?
    result[key].push word

Ruby Solution

words = ["abc", "bca", "mkzp", "cba"]
words.group_by { |w| w.chars.sort.join }
comments powered by Disqus