-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class Solution { | ||
public int minSteps(String s, String s1) { | ||
int[] freq1 = new int[26]; | ||
int[] freq2 = new int[26]; | ||
|
||
// Count the frequency of characters in the first string | ||
for (char c : s.toCharArray()) { | ||
freq1[c - 'a']++; | ||
} | ||
|
||
// Count the frequency of characters in the second string | ||
for (char c : s1.toCharArray()) { | ||
freq2[c - 'a']++; | ||
} | ||
|
||
int delete = 0; | ||
|
||
// Calculate the difference in character frequencies | ||
for (int i = 0; i < 26; i++) { | ||
delete += Math.abs(freq1[i] - freq2[i]); | ||
} | ||
return delete; | ||
|
||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/sol.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Easiest solution to understand the solution | ||
|
||
1. Create two arrays of 26 Characters | ||
2. Count the frequency of characters in the first string | ||
3. Count the frequency of characters in the second string | ||
4. Calculate the difference in character frequencies | ||
5. Return the difference Value | ||
|
||
Example 1: | ||
|
||
Input: s = "leetcode", t = "coats" | ||
|
||
Output: 7 | ||
|
||
Example 2: | ||
|
||
Input: s = "night", t = "thing" | ||
|
||
Output: 0 |