From c02fa16282a5583e5539371719139d676928ab0e Mon Sep 17 00:00:00 2001 From: dadgam3er Date: Fri, 25 Oct 2024 23:12:19 -0400 Subject: [PATCH] MakeAnagramMethod2 --- MakeAnagram.java | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 MakeAnagram.java diff --git a/MakeAnagram.java b/MakeAnagram.java new file mode 100644 index 0000000..f976fe8 --- /dev/null +++ b/MakeAnagram.java @@ -0,0 +1,49 @@ +import java.util.Scanner; + +/** + * MakeAnagram + */ +public class MakeAnagram { + + // Anagran is 2 words compose of the sames letters such LISTEN and SILENT, MUG + // and GUM, RATE and TEAR etc... + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Please enter the first String here: " + "\t"); + String str1 = sc.nextLine(); + System.out.print("Please enter the second String here: " + "\t"); + String str2 = sc.nextLine(); + sc.close(); + + System.out.println(DeletionCount(str1, str2)); + + } + + private static int DeletionCount(String strOne, String strTwo) { + int deletionCount = 0; + + int[] Freqency = new int[26]; + strOne = strOne.toLowerCase(); + strTwo = strTwo.toLowerCase(); + + // fill the frequency for strOne chars + for (int i = 0; i < strOne.length(); i++) { + Freqency[strOne.charAt(i) - 'a']++; + } + + // fill the frequency for strTwo chars + for (int i = 0; i < strTwo.length(); i++) { + Freqency[strTwo.charAt(i) - 'a']--; + } + + // to calculate the deletion count we are going to do the absolute addition of + // the strOne and strTwo + + for (int x : Freqency) { + deletionCount += Math.abs(x); + } + + return deletionCount; + } +}