MakeAnagram

This commit is contained in:
dadgam3er 2024-10-25 19:05:45 -04:00
parent 019f62504b
commit 322aa305a3
3 changed files with 38 additions and 4 deletions

Binary file not shown.

View File

@ -1,12 +1,42 @@
import java.util.Scanner;
public class AnagramPeriod {
public static void main(String[] args) {
getAnagramPeriod("ababbaab");
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 void getAnagramPeriod(String input_str) {
int n = input_str.length();
System.out.println(n);
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;
}
}

View File

@ -0,0 +1,4 @@
{
"status": "failed",
"failedTests": []
}