Minimum Number of Steps to Make Two Strings Anagram LeetCode solution in Java

1
// find the frequency of every letter and check diffrence between the frequency of each letter then
2
// divide it by 2 to calculate the minimum number of letter to be changed.
3
class Solution {
4
public int minSteps(String s, String t) {
5
int sf[] = new int[26];
6
int tf[] = new int[26];
7
int diff = 0;
8
for (char c : s.toCharArray()) {
9
sf[c - 'a']++;
10
}
11
for (char c : t.toCharArray()) {
12
tf[c - 'a']++;
13
}
14
for (int i = 0; i < 26; i++) {
15
diff += (int) Math.abs(sf[i] - tf[i]);
16
}
17
return diff / 2;
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0