171.Anagrams

1.Description(Medium)

Given an array of strings, return all groups of strings that are anagrams.

Notice

All inputs will be in lower-case

Example

Given["lint", "intl", "inlt", "code"], return["lint", "inlt", "intl"].

Given["ab", "ba", "cd", "dc", "e"], return["ab", "ba", "cd", "dc"].

Challenge

What is Anagram?

  • Two strings are anagram if they can be the same after change the order of characters.

Tags

Hash Table String Uber Facebook

2.Code

Version 1:排序哈希表法:时间 O(NKlogK) 空间 O(N) ; 我们有n个字符串,字符串最大长度是k(排序用klogk)

判断两个词是否是变形词,最简单的方法是将两个词按字母排序,看结果是否相同。这题中我们要将所有同为一个变形词词根的词归到一起,最快的方法则是用哈希表。所以这题就是结合哈希表和排序。

我们将每个词变成数组,然后为数组排序,再变回string.

根据这个键值,找到哈希表中相应的列表,并添加进去。

为了满足题目字母顺序的要求,我们输出之前还要将每个列表按照内部的词排序一下。可以直接用Java的Collections.sort()这个API。

注意在char[] 变回string的时候不能用.toString();

这个方法过不了当input是["",""],output是[], 但实际应该是["",""]

应该用String key=String.valueOf(current); 或者String key=new String(current);

String.valueOf 和toString()的区别:

When argument is null , the String.valueOf returns "null" ,

but Object.toString throws NullPointerException , that's the only difference.

Version 2: 为每一个单词建立一个hash值,用hash值存放进hashmap.

Last updated

Was this helpful?