Hash Tables C++
Bob and Carl both have words. Bob has a word S and Carl has a word T. They want to make both words S and T into anagrams of each other. Carl can apply two operations to convert word T to anagram of word S which are given below:
1.) Delete one character from the word T.
2.) Add one character from the word S.
Carl can apply above both operation as many times he wants. Find the minimum number of operations required to convert word T so that both T and S will be anagrams of each other.
Input:
First line of input contains number of test cases T. Each test case contains two lines. First line contains string S and second line contains string T.
Output:
For each test case print the minimum number of operations required to convert string T to anagram of string S.
Sample Input
4 abc cba abd acb talentpad talepdapd code road
Output for sample input
0 2 4 4
Hello! Firsly lets understand the solution then you can find the code below with attached screenshot. Comments are also written within code. Try to read those also. It passes all your given question test cases. Try with other test cases too. Hope those will also be passed by this solution.
First step is to sort both the strings. Once both strings are sorted you can now find the LCS(longest common subsequence) this tells how many characters are similar in both strings. Now we want to delete not similar characters from 'T'. So we have to perform Length(T) - LCS(S,T) operations , this will delete all dissimilar characters from T. Now we have to copy all the disimilar characters from S to T. Thus we have to perform Length(S) - LCS(S,T) operations. Add both operations you will get the answer. Now you can copy the code , compile it and then run it according to question.
Get Answers For Free
Most questions answered within 1 hours.