![]() |
VOOZH | about |
Given two strings in lowercase, the task is to make them anagrams. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagrams of each other if one of them can be converted into another by rearranging its letters.
Examples :
Input : s1 = "bcadeh", s2 = "hea"
Output: 3
Explanation: We need to remove b, c and d from s1.
Input : s1 = "cddgk", s2 = "gcda"
Output: 3
Explanation: Remove one occurrence of d and k from s1 and one occurrence of a from s2.
Input : s1 = "bca", s2 = "acb"
Output: 0
Explanation: Strings are already anagrams.
Table of Content
The idea is to use frequency counting to determine the number of characters that need to be removed from both strings to make them anagrams. By comparing the frequency of each character in both strings, we can calculate the total number of deletions required to balance the frequencies.
Step by step approach:
3
The idea is to use one count array, increment frequencies for first string and decrease for second string.
3
The idea is similar to the first two approaches, but instead of using an array, hash map is used. This approach is useful when the strings can contain different types of characters (lowercase, uppercase, special symbols).
3