forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
49 lines (45 loc) · 1.43 KB
/
GroupAnagrams.java
File metadata and controls
49 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package LeetCode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GroupAnagrams {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>>result=new ArrayList<>();
HashMap<String,List<String>> res=new HashMap<>();
for (String s:strs){
List<String> t=getAnagrams(s);
int flag=0;
for(String tt:t){
if(res.keySet().contains(tt)){
res.get(tt).add(s);
flag=1;
break;
}
}
if(flag==0){
List<String> l=new ArrayList<>();
l.add(s);
res.put(s,l);
}
}
result.addAll(res.values());
return result;
}
public List<String> getAnagrams(String s){
List<String> temp=new ArrayList<>();
for(int i=0;i<s.length();i++){
temp.add(s.substring(i)+s.substring(0,i));
}
String s2=new StringBuffer(s).reverse().toString();
temp.add(s2);
for(int i=0;i<s.length();i++){
temp.add(s.substring(i)+s.substring(0,i));
}
return temp;
}
public static void main(String[] args){
GroupAnagrams groupAnagrams=new GroupAnagrams();
String [] test={"eat", "tea", "tan", "ate", "nat", "bat"};
System.out.println(groupAnagrams.groupAnagrams(test));
}
}