Similar Problem:
You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.
Input: s = "bab", t = "aba"
Output: 1
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
The goal of this problem is to find out the difference in charcters in s and t.
- since it's the anagram of both, their length must be same
- use freq char array to count the difference in characters
Time: O(n+m)
Space: O(26) = O(1)
public int minSteps(String s, String t) {
int [] freq = new int[26];
int diff = 0;
for (char c: s.toCharArray())
freq[c-'a']++;
for (char c: t.toCharArray()) {
freq[c-'a']--;
if (freq[c-'a'] < 0)
diff++;
}
return diff;
}