codeChallenge/AnagramPeriod/AnagramPeriod.java
2024-10-25 19:05:45 -04:00

43 lines
1.0 KiB
Java

import java.util.Scanner;
public class AnagramPeriod {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
sc.close();
int totalDel = makeAnagram(a, b);
System.out.println(totalDel);
}
private static int makeAnagram(String strOne, String strTwo) {
// bacdc
// dcbac
// bacdc
// dcbad
int[] a_frequency = new int[26]; // 26 numbers of letters in the alphabet.
int[] b_frequency = new int[26]; // 26 numbers of letters in the alphabet.
// populate the frequence array for String strOne
for (char c : strOne.toCharArray()) {
a_frequency[c - 'a']++;
}
// populate the frequence array for String strTwo
for (char c : strTwo.toCharArray()) {
b_frequency[c - 'a']++;
}
// calculate the total number of deletions needed
int deletion = 0;
for (int i = 0; i < 26; i++) {
deletion += Math.abs(a_frequency[i] - b_frequency[i]);
}
return deletion;
}
}