-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnagram.java
More file actions
38 lines (31 loc) Β· 1.15 KB
/
Copy pathAnagram.java
File metadata and controls
38 lines (31 loc) Β· 1.15 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
package Chapter7.Day45;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Anagram {
public static void main(String[] args) throws FileNotFoundException {
File dictionary = new File(args[0]);
int minGroupSize = Integer.parseInt(args[1]);
Map<String, Set<String>> groups = new HashMap<>();
try (Scanner s = new Scanner(dictionary)) {
while (s.hasNext()) {
String word = s.next();
groups.computeIfAbsent(alphabetize(word), unused -> new TreeSet<>()).add(word);
}
}
/**
* computeIfAbsent
* ν€ κ°μ΄ μμΌλ©΄ λ§€νλ κ° λ°ν
* μμΌλ©΄ ν¨μ κ°μ²΄λ₯Ό ν€μ μ μ©νμ¬ κ°μ κ³μ°νκ³ , ν€κ°μ λ§€νν λ€μ κ³μ°λ κ° λ°ν
*/
for (Set<String> group : groups.values()) {
if (group.size() == minGroupSize)
System.out.println(group.size() + ": " + group);
}
}
private static String alphabetize(String word) {
char a[] = word.toCharArray();
Arrays.sort(a);
return new String(a);
}
}