-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
40 lines (33 loc) · 1.24 KB
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
public class Solution {
private static final int NUM_LETTERS = 26; // we assume lower-case letters only
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
System.out.println(numberNeeded(a, b));
}
private static int numberNeeded(String first, String second) {
/* For each string, create an array of the count of each character */
int [] array1 = createFilledArray(first);
int [] array2 = createFilledArray(second);
/* Count number of deletions we need to make the Strings anagrams of each other */
int deletions = 0;
for (int i = 0; i < NUM_LETTERS; i++) {
deletions += Math.abs(array1[i] - array2[i]);
}
return deletions;
}
/* Creates an array with the count of each character */
private static int [] createFilledArray(String str) {
int [] array = new int[NUM_LETTERS];
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
array[ch - 'a']++;
}
return array;
}
}