-
Notifications
You must be signed in to change notification settings - Fork 0
/
1347. Minimum Number of Steps to Make Two Strings Anagram.java
46 lines (39 loc) · 1.44 KB
/
1347. Minimum Number of Steps to Make Two Strings Anagram.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
LC: https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram
Approach : Store the freq. of character of both string individually. Now the task is we have to make the frq. of character of String T equal with String S for anagram.
So, if frq[c] in String S > frq[c] in String T , just add (frq[c] in S-frq[c] in T) to ans.
class Solution {
public int minSteps(String s, String t) {
int n=s.length();
int frq1[]=new int[26];
int frq2[]=new int[26];
for(int i=0;i<n;i++){
frq1[s.charAt(i)-'a']++;
frq2[t.charAt(i)-'a']++;
}
int ans=0;
for(int i=0;i<26;i++){
if(frq1[i]>frq2[i]){
ans+=(frq1[i]-frq2[i]);
}
}
return ans;
}
}
Approach - 2 (Using HashMap)
class Solution {
public int minSteps(String s, String t) {
int[] count = new int[26];
// Storing the difference of frequencies of characters in t and s.
for (int i = 0; i < s.length(); i++) {
count[t.charAt(i) - 'a']++;
count[s.charAt(i) - 'a']--;
}
int ans = 0;
// Adding the difference where string t has more instances than s.
// Ignoring where t has fewer instances as they are redundant and can be covered by the first case.
for (int i = 0; i < 26; i++) {
ans += Math.max(0, count[i]);
}
return ans;
}
}