forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
49 lines (35 loc) · 1.52 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
41
42
43
44
45
46
47
48
49
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// makingAnagrams function
static int makingAnagrams(String s1, String s2) {
int[] freqs1 = new int[26];
int[] freqs2 = new int[26];
int deletion = 0;
for(int i = 0; i < s1.length(); i++)
freqs1[s1.charAt(i)-'a'] = freqs1[s1.charAt(i)-'a'] + 1; //Increment frequency of char at i
for(int i = 0; i < s2.length(); i++)
freqs2[s2.charAt(i)-'a'] = freqs2[s2.charAt(i)-'a'] + 1; //Increment frequency of char at i
for(int i = 0; i < 26; i++)
deletion += Math.abs(freqs1[i] - freqs2[i]); //Track the total deletions needed
// minimum number of characters which must be deleted to make the two strings anagrams of each other
return deletion;
}
private static final Scanner scanner = new Scanner(System.in);
// main function
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s1 = scanner.nextLine(); // first string
String s2 = scanner.nextLine(); // second string
int result = makingAnagrams(s1, s2); // function call
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}