-
Notifications
You must be signed in to change notification settings - Fork 0
/
049_GroupAnagrams49.java
179 lines (160 loc) · 5.1 KB
/
049_GroupAnagrams49.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res=new ArrayList<List<String>>();
List<String> s=new ArrayList<>();
Collections.addAll(s,strs);
while(s.size()!=0){
List<String> temp=new ArrayList<>();
temp.add(s.get(0));
for(int i=1;i<s.size();){
if(isAnagram(temp.get(0),s.get(i))){
temp.add(s.get(i));
s.remove(s.get(i));
}else
i++;
}
res.add(temp);
s.remove(temp.get(0));
}
return res;
}
public boolean isAnagram(String s1,String s2){
if(s1.length()==0 && s2.length()==0)
return true;
if(s1.length()==0 || s2.length()==0 || s1.length()!=s2.length())
return false;
char[] a1=s1.toCharArray();
char[] a2=s2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
for (int i=0;i<a1.length;i++)
if (a1[i]!=a2[i])
return false;
return true;
}
}
****************************************************************************************************
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String s:strs){
//String key = getAnagramKey(s);
String key = getSortKey(s);
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(s);
}
//System.out.println(map);
List<List<String>> res = new ArrayList<>();
for(List<String> curr : map.values()){
res.add(curr);
}
return res;
}
private String getSortKey(String s)
{
char[] temp = s.toCharArray();
Arrays.sort(temp);
return new String(temp);
}
private String getAnagramKey(String s)
{
Map<Character, Integer> map = new TreeMap<>();
for(int i=0;i<s.length(); i++){
map.put(s.charAt(i), map.getOrDefault(s.charAt(i),0)+1);
}
StringBuilder sb = new StringBuilder();
for(Map.Entry<Character, Integer> entry : map.entrySet()){
sb.append(entry.getValue());
sb.append(entry.getKey());
}
return sb.toString();
}
}
***************************************************************
/**
* Given an array of strings strs, group the anagrams together. You can return the answer in any order.
* An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all
* the original letters exactly once.
*
* Example 1:
* Input: strs = ["eat","tea","tan","ate","nat","bat"]
* Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
*
* Example 2:
* Input: strs = [""]
* Output: [[""]]
*
* Example 3:
* Input: strs = ["a"]
* Output: [["a"]]
*
* Constraints:
* 1 <= strs.length <= 104
* 0 <= strs[i].length <= 100
* strs[i] consists of lower-case English letters.
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> groups = new HashMap<>();
for (int i = 0; i < strs.length; i++) {
String enc = encode(strs[i]);
if (groups.containsKey(enc)) {
groups.get(enc).add(strs[i]);
} else {
groups.put(enc, new ArrayList<String>(Arrays.asList(strs[i])));
}
}
List<List<String>> res = new ArrayList<>();
for (String key : groups.keySet()) {
res.add(groups.get(key));
}
return res;
}
private String encode(String str) {
Queue<Character> pq = new PriorityQueue<>();
Map<Character, Integer> m = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (m.containsKey(c)) {
m.put(c, m.get(c) + 1);
} else {
m.put(c, 1);
pq.offer(c);
}
}
String res = "";
while (!pq.isEmpty()) {
char c = pq.poll();
res += c + "" + m.get(c);
}
return res;
}
}
************************************************************************
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<>();
for(int i=0;i<strs.length;i++){
String k= hash(strs[i]);
if(!map.containsKey(k)) {
map.put(k,new LinkedList<>());
}
map.get(k).add(strs[i]);
}
return new ArrayList<>(map.values());
}
private String hash(String str) {
int[] ch = new int[26];
StringBuilder h = new StringBuilder();
for(int i=0;i<str.length();i++){
ch[str.charAt(i)-'a']+=1;
}
for(int i=0;i<ch.length;i++){
if(ch[i]!=0){
h.append(ch[i]);
h.append((char)'a'+i);
}
}
return h.toString();
}
}