Bob and String

Bob and Khatu both love the string. Bob has a string S and Khatu has a string T. They want to make both strings S and T to anagrams of each other. Khatu can apply two operations to convert string T to an anagram of string S which are given below:

1.) Delete one character from the string T.
2.) Add one character from the string S.

Khatu can apply above both operations as many times as he wants. Find the minimum number of operations required to convert string T so that both T and S will become anagram of each other.

Example:

Input: s = "talentpad", t = "talepdapd"
Output: 4

Approach

C++

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

int bobAndString(string sstring t)
{
    int f[26] = {0};
    int f1[26] = {0};
    for (int i = 0i < s.size(); i++)
        f[s[i] - 'a']++;
    for (int i = 0i < t.size(); i++)
        f1[t[i] - 'a']++;
    int ans = 0;
    for (int i = 0i < 26i++)
        ans += abs(f[i] - f1[i]);
    return ans;
}
int main()
{

    string s = "talentpad"t = "talepdapd";

    cout << bobAndString(st<< "\n";

    return 0;
}


No comments:

Post a Comment