-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
48 lines (42 loc) · 1.22 KB
/
Result.java
File metadata and controls
48 lines (42 loc) · 1.22 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
39
40
41
42
43
44
45
46
47
48
package hackrank.algorithm.string.mkanagram;
import java.util.Arrays;
/**
* @see <a href="https://www.hackerrank.com/challenges/making-anagrams">Making Anagrams</a>
*/
public class Result {
/**
* @param s1 first string value
* @param s2 second string value
* @return Minimum number of deletions needed from {@code s1} and/or {@code s2} to make an anagram
*/
public static int makingAnagrams(String s1, String s2) {
char[] c1 = s1.toCharArray();
Arrays.sort(c1);
char[] c2 = s2.toCharArray();
Arrays.sort(c2);
int i = 0;
int j = 0;
int deletions = 0;
while (i < c1.length || j < c2.length) {
if (i < c1.length && j < c2.length) {
if (c1[i] == c2[j]) {
i++;
j++;
} else if (c1[i] < c2[j]) {
i++;
deletions++;
} else {
j++;
deletions++;
}
} else if (i < c1.length) {
i++;
deletions++;
} else {
j++;
deletions++;
}
}
return deletions;
}
}