-
Notifications
You must be signed in to change notification settings - Fork 3
/
SherlockAndAnagrams.java
48 lines (42 loc) · 1.78 KB
/
SherlockAndAnagrams.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
// https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem
package strings;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class SherlockAndAnagrams {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
while (queries-- > 0) {
String string = scanner.next();
System.out.println(numberOfAnagrammaticPairs(string));
}
}
private static int numberOfAnagrammaticPairs(String string) {
Map<Map<Character, Integer>, Integer> frequencyDistribution = getFrequenciesOfSubstrings(string);
int anagrammaticPairs = 0;
for (int frequency : frequencyDistribution.values()) {
anagrammaticPairs += (frequency * (frequency - 1)) / 2;
}
return anagrammaticPairs;
}
private static Map<Map<Character, Integer>, Integer> getFrequenciesOfSubstrings(String string) {
Map<Map<Character, Integer>, Integer> frequencies = new HashMap<>();
char[] array = string.toCharArray();
for (int i = 0 ; i < array.length ; i++) {
for (int j = i + 1 ; j <= array.length ; j++) {
Map<Character, Integer> signature = getFrequency(array, i, j);
frequencies.put(signature, frequencies.getOrDefault(signature, 0) + 1);
}
}
return frequencies;
}
private static Map<Character, Integer> getFrequency(char[] array, int start, int end) {
Map<Character, Integer> frequency = new HashMap<>();
for (int index = start ; index < end ; index++) {
char character = array[index];
frequency.put(character, frequency.getOrDefault(character, 0) + 1);
}
return frequency;
}
}