开发者

Group together all the anagrams [closed]

开发者 https://www.devze.com 2023-01-14 05:25 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help cla开
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help cla开发者_开发问答rifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Problem statement: You are given a set of k strings, each length n. You have to output the group of anagrams together. Anagrams are like e.g atm - mat , like-kile.


Just sort the word's letters to obtain a signature that's anagram-specific. E.g., in Python,

sig = ''.join(sorted(word))

and make a dict with sig as the key and the value being a list of words with that signature (defaultdict(list) works well for this). Of course, you can do it in any language with sorting abilities, and associative arrays whose values can be lists or vectors;-).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号