Question

Anagram Difference We define an anagram to be a word whose characters can be rearranged to create another word. Given two str
media%2Fc75%2Fc75a239b-a1d4-460a-aac1-0c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A simple strategy is used for calculating the minimum difference to make anagram i.e. count the characters in the first string and then iterate through the second string and minus the similar characters and add the remaining characters . this strategy is used in the following program

Here is the code:

#include<bits/stdc++.h>
using namespace std;
vector<int> getMinimumDifference(std::vector<string> a,std::vector<string> b)
{
std::vector<string> ::iterator ita;
std::vector<string> ::iterator itb;
std::vector<int> difference;
for(ita=a.begin(),itb=b.begin();ita!=a.end() && itb!= b.end(); ita++ , itb++)
{
string s1=*ita;
string s2=*itb;
int count = 0;
  
// store the count of character
int char_count[26];
//intialize with the 0   
for (int i = 0; i < 26; i++){
char_count[i] = 0;
}
if(s1.length()!=s2.length()) // when lengths are not equal then we can not make anagram so push -1
{
difference.push_back(-1);
continue;
}
// iterate though the first String
// and update count
for (int i = 0; i < s1.length(); i++)
char_count[s1[i] - 'a']++; // this s1[i]-'a' will map a to 0
  
// iterate through the second string
// update char_count.
// if character is not found in
// char_count then increase count
for (int i = 0; i < s2.length(); i++){
char_count[s2[i] - 'a']--;
if (char_count[s2[i] - 'a'] < 0)
count++;
}
difference.push_back(count);

}
return difference;

}
int main()
{
std::vector<string> a;
std::vector<string> b;
// some dummy data for testing
a.push_back("tea");
b.push_back("toe");
a.push_back("ast");
b.push_back("astb");
a.push_back("anki");
b.push_back("hell");

std::vector<int> difference=getMinimumDifference(a,b);
std::vector<int>:: iterator it;
std::vector<string> ::iterator ita;
std::vector<string> ::iterator itb;
for(it=difference.begin(),ita=a.begin(),itb=b.begin();it!=difference.end();it++,ita++,itb++)
cout<<*ita<<"\t"<<*itb<<"\t"<<*it<<"\n";
}

here is the screenshot of the program

1 #include<bits/stdc++.h> 2 using namespace std; 3 vector<int getMinimumDifference(std::vector<string> a,std::vector<string>

9 30 // iterate through the second string // update char count // if character is not found in / char count then increase cou

here is the output of the program:

PS D:\mingw-w64\1686-8.1.0-posix-dwarf-rt-v6_rev0\mingw32\bin> ./g++ digi .cpp PS D: \mingw-w64 1686-8.1.0-posix-dwarf-rt_v6-

I hope this will help you so please give positive ratings :))

Add a comment
Know the answer?
Add Answer to:
Anagram Difference We define an anagram to be a word whose characters can be rearranged to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Two words or phrases in English are anagrams if their letters (and only their letters), rearranged,...

    Two words or phrases in English are anagrams if their letters (and only their letters), rearranged, are the same. We assume that upper and lower case are indistinguishable, and punctuation and spaces don't count. Two phrases are anagrams if they contain exactly the same number of exactly the same letters, e.g., 3 A's, 0 B's, 2 C's, and so forth. Some examples and non-examples of regular anagrams: * The eyes / they see (yes) * moo / mo (no) *...

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • 2. This question is about processing strings 2.1 [4 marks] Given a string str, we can...

    2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...

  • In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...

    In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that...

    Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I am sure we’ve all done this many times when working with Word, Notepad, or other editors. Since we don’t have a GUI or other means of displaying the contents of a file all at once, let’s modify the problem slightly. Rather than locating a specific substring within a...

  • PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge...

    PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge of strings, files, loops, and arrays. In this homework, you must write a C++ program that asks the user for an input and output file. For each line in the input file, write a modified line containing a line number to the output file. Additionally, calculate the number of paragraphs, lines, words, and characters. Write the summary information to the bottom of the output...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • C++. Need some help getting started. We will also have the following two functions: 1. A...

    C++. Need some help getting started. We will also have the following two functions: 1. A mutate function that randomly modifies a chromosome. 2. A crossover function that takes two chromosomes and splits each one at the same spot, then combines them together. Our genetic algorithm works by iterating over generations of chromosomes via the following process: 1. Generate random population. 2. Until we get an answer that is good enough, do the next steps in a loop: (a) Do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT