Anagrams

Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.

Example:

Input:  a = "cde", b = "abc"
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int anagrams(string astring b)
{
    int f[26] = {0}, f1[26] = {0};
    for (int i = 0i < a.size(); i++)
    {
        f[a[i] - 'a']++;
    }
    for (int i = 0i < b.size(); i++)
    {
        f1[b[i] - 'a']++;
    }
    int sum = 0;
    for (int i = 0i < 26i++)
        sum += abs(f[i] - f1[i]);
    return sum;
}
int main()
{

    string a = "cde"b = "abc";

    cout << anagrams(ab<< "\n";

    return 0;
}


No comments:

Post a Comment