Question

Hash Tables C++ Bob and Carl both have words. Bob has a word S and Carl...

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

Homework Answers

Answer #1

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.

  1. #include<bits/stdc++.h>   
  2. using namespace std;   
  3.   
  4. int max(int a, int b);   
  5.   
  6. /* Returns length of LCS for string X and Y */  
  7. int lcs( string X, string Y, int m, int n )   
  8. {   
  9.     int L[m + 1][n + 1];   
  10.     int i, j;   
  11.       
  12.     /* Following steps build L[m+1][n+1] in  
  13.     bottom up fashion. Note that L[i][j]  
  14.     contains length of LCS of X[0..i-1]  
  15.     and Y[0..j-1] */  
  16.     for (i = 0; i <= m; i++)   
  17.     {   
  18.         for (j = 0; j <= n; j++)   
  19.         {   
  20.         if (i == 0 || j == 0)   
  21.             L[i][j] = 0;   
  22.       
  23.         else if (X[i - 1] == Y[j - 1])   
  24.             L[i][j] = L[i - 1][j - 1] + 1;   
  25.       
  26.         else  
  27.             L[i][j] = max(L[i - 1][j], L[i][j - 1]);   
  28.         }   
  29.     }   
  30.           
  31.     /* L[m][n] contains length of LCS  
  32.     for X[0..n-1] and Y[0..m-1] */  
  33.     return L[m][n];   
  34. }   
  35.   
  36. /* Utility function to get max of 2 integers */  
  37. int max(int a, int b)   
  38. {   
  39.     return (a > b)? a : b;   
  40. }   
  41.   
  42. // Driver Code   
  43. int main()   
  44. {  
  45.   //Enter the number of test cases   
  46.     int T;  
  47.     cin>>T;  
  48.   
  49.     while(T--)  
  50.     {  
  51.     //Enter the string S and then String T    
  52.     string s,t;  
  53.     cin>>s>>t;  
  54.       
  55.       
  56.     //Getting the length of both strings in m and n variable.  
  57.     int m = s.length();   
  58.     int n = t.length();   
  59.       
  60.   
  61.     //sorting the strings for running the lCS  
  62.     sort(s.begin(),s.end());  
  63.     sort(t.begin(),t.end());  
  64.       
  65.     int l = lcs( s, t, m, n );  
  66.       
  67.     //output the final result  
  68.     cout<<(n-l)+(m-l)<<"\n";  
  69.       
  70.     }  
  71.     return 0;   
  72. }   
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ PROJECT Objectives • To solve problems using vectors • To apply sorting Introduction Two words...
C++ PROJECT Objectives • To solve problems using vectors • To apply sorting Introduction Two words are anagrams of each other if one can be produced by a reordering of letters of the other. For example, “resistance” and “ancestries” are a pair of anagrams. Another anagram pair is “admirer” and “married”. Each anagram forms an equivalence relation since it is: • reflexive (each word is an anagram of itself) • symmetric (if w1 is an anagram of w2 then w2...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Language C++ // For an input string of words, find the most frequently occuring word. In...
Language C++ // For an input string of words, find the most frequently occuring word. In case of ties, report any one of them. // Your algorithm should be O(n) time where n is the number of words in the string #include <iostream> #include <vector> #include <sstream> using namespace std; string findWord(vector<string>& tokens); int main() { string line = "Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as an input. The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string. Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together. Let the frequency of a character, lying in...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on using arrays. Instructions Create a program called arrays.c that does the following: 1. Prompt the user for a string (<= 100 characters). (using the attached file: AS06Data.txt) AS06Data.txt file contains the text " Instructions: When your executing program displays "Enter string 1:" starting with T, select the line of text below, copy with ctrl-c, then type ctrl-v THIS IS a test of string 0123456789...
This is an intro to Java Question. My current solution is giving me bad outputs. Please...
This is an intro to Java Question. My current solution is giving me bad outputs. Please show me your way of solving this. Problem 4: Min/Max Search by Value Develop a program that, given a sequence S of integers as input, produces as two output values, the first is the minimum value that appears in the sequence and the second is the maximum value that appears in the sequence. Facts ● Scanner has a method that returns a boolean indicating...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT