-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSherlockAndAnagrams.java
47 lines (44 loc) · 1.42 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
/*https://www.hackerrank.com/challenges/sherlock-and-anagrams/*/
class Result {
public static int sherlockAndAnagrams(String text) {
// Write your code here
ArrayList<String> list = new ArrayList<>();
int len = text.length();
int counter = 0;
//add all substrings to arraylist
for (int j = 0; j < len; ++j)
{
for (int sub = 1; sub <= len-j; ++sub)
{
String subPart = text.substring(j,j+sub);
list.add(subPart);
}
}
for (int j = 0; j < list.size(); ++j)
{
String element = list.get(j); //for each substring in the list
for (int k = j+1; k < list.size(); ++k) //for all remaining substrings
{
if (isAnagram(list.get(k), element)) //if anagrams, increment counter
counter++;
}
}
return counter;
}
public static boolean isAnagram(String a, String b){
if (a.length() != b.length())
return false;
int[] lettermap = new int[26];
for (int j = 0; j < a.length(); ++j)
{
char ch = a.charAt(j);
++lettermap[ch-'a'];
ch = b.charAt(j);
--lettermap[ch-'a'];
}
for (int j = 0; j < lettermap.length; ++j)
if (lettermap[j] != 0)
return false;
return true;
}
}