Skip to content

Commit

Permalink
Simple solution to Anagram II
Browse files Browse the repository at this point in the history
  • Loading branch information
oyerounak committed Oct 7, 2023
1 parent 03b9e19 commit 3f6dccc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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;

}

}
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

0 comments on commit 3f6dccc

Please sign in to comment.