Anagram Problem - Grouping all anagrams [reviewed]

 Question

 Given a list of strings, return a list of lists of strings that groups all anagrams.
  Ex. given {trees, bike, cars, steer, arcs}

  return {{cars, arcs}, {bike}, {tree, steer}}

  m = # of words
  n = length of longest word


  Anagrams are the works with same set of letters but in different orders. This means if we can sort individual words, we can easily find out whether two words are the same.

  3:43 min : to think up the solution. But misunderstood the problems
  13:58 min: to code up the solution based on misunderstanding.
  21 secs: to found out the problem during the verification and fix it up.

  First, I overlooked the problem misled by the keyword, anagram. This should not happen.

  Secondly, did not know C++ solution for string sorting, which I know now. std::sort(begin(), end());


Here is the code in C++.
End of the code.


UPDATE(2019-07-31): solved the question again in python. Got the code right but did not know how to short chars in the string with python. Had to get an help from ipython console.

-
Practice statistic

09:57: to think of algorithm and write up the code
04:40: to fix the sorting logic.

Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots

Stock price processing problem