Question

IN C++: Input to all three functions is a vector. You need to return an ana_result...

IN C++:
Input to all three functions is a vector.

You need to return an ana_result struct which will have its found field set to false if there are no anagrams in the input and true otherwise. If an anagram is found, then the s1 and s2 fields should be set to the strings. For anagram checking, you may just sort the string’s characters and compare for equality (assumption is that there are many short strings).

anyAnagramsHash()

  • Use std::map to sort the vector of strings and see if there are any anagrams.

  • There are several strategies here.

  • It is important to remember that you must return the original, unsorted strings if you find anagrams. You are sorting both the entire vector (presorting strategy) and, probably, the strings individually for comparison purposes.

  • Defining your own comparison function is likely necessary. Lambda functions are desirable here.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct anagram{

string str1;
string str2;
bool found;

};

anagram f(anagram temp)
{
string s1=temp.str1;
string s2=temp.str2;

int a=0;
int b=0;

map<char,int> M1;
map<char,int> M2;

while(s1[a]!='\0')
{
M1[s1[a]]++;
a++;
}

while(s2[b]!='\0')
{
M2[s2[b]]++;
b++;
}

if(M1.size()!=M2.size())
{temp.found=false;
return temp;}

map<char,int>::iterator itr1;
map<char,int>::iterator itr2;

for(itr1=M1.begin(),itr2=M2.begin();itr1!=M1.end(),itr2!=M2.end();itr1++,itr2++)
{
if(itr1->first!=itr2->first || itr1->second!=itr2->second)
{
temp.found=false;
return temp;
}

}
temp.found=true;
return temp;
}

int main()

{
anagram temp;
temp.str1="silent";
temp.str2="listen";

anagram ana_result=f(temp);
cout<<ana_result.found;

}

Add a comment
Know the answer?
Add Answer to:
IN C++: Input to all three functions is a vector. You need to return an ana_result...
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
  • Program this in C There is a lot of sorting algorithms. So far, we have covered...

    Program this in C There is a lot of sorting algorithms. So far, we have covered selection and insertion sort. However, we only covered how selection sort is implemented using an array. Every sorting algorithm implemented using an array can also be implemented using a linked list. In this assignment, you will implement the selection sort algorithm using a linked list instead of an array. You will first create an interface for handling string items. Basically, you will need to...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • All to be done in C++ no user input is required. Just need to create vectors...

    All to be done in C++ no user input is required. Just need to create vectors and create functions to manipulate them as described. Search 2D Vector - Part 2 Write a C++ program that uses a function to search a 2D vector of floats. The function will have 2 parameters: 1. A 2 dimensional vector of floats, v 2. The floating point number that you are searching for, item Rewrite the Search_2D_Vector function so that it can work with...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • C++ requirements All values must be read in as type double and all calculations need to...

    C++ requirements All values must be read in as type double and all calculations need to be done using type double. For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview This program will convert a set...

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