MakeAnagramMethod2
This commit is contained in:
parent
322aa305a3
commit
c02fa16282
49
MakeAnagram.java
Normal file
49
MakeAnagram.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user