-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
42 lines (41 loc) · 1.26 KB
/
Solution.java
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
import java.util.*;
public class Solution {
/**
* @param strs: A list of strings
* @return: A list of strings
*/
public static List<String> anagrams(String[] strs) {
// write your code here
//char[] A = new char[256];
if(strs.length <= 1 ){
return null;
}
HashMap<char[] , LinkedList<String>> store = new HashMap<char[] , LinkedList<String>>();
for(int i = 0;i < strs.length; i++){
char[] A = strs[i].toCharArray();
System.out.println("PRE" +A);
Arrays.sort(A);
if(store.containsKey(A)){
store.get(A).add(strs[i]);
System.out.println(A);
}
else{
System.out.println("AFTER"+A);
LinkedList<String> S = new LinkedList<>();
S.add(strs[i]);
store.put(A , S);
}
}
LinkedList<String> result = new LinkedList<String>();
for(LinkedList L : store.values()){
if(L.size() > 1){
result.addAll(L);
}
}
return result;
}
public static void main(String[] args){
String[] strs = {"",""};
System.out.println(anagrams(strs));
}
}