Question: Recognizing anagrams Given a Vector phrases, remove each phrase that is an anagram of an earlier phrase, and return the remaining phrases in their original
Recognizing anagrams
Given a Vector
Examples:
{ "SnapDragon vs tomek", "savants groped monk", "Adam vents prongs ok" } Returns: { "SnapDragon vs tomek" } { "Aaagmnrs", "TopCoder", "anagrams", "Drop Cote" } Returns: { "Aaagmnrs", "TopCoder" } The code in bold CANNOT be changed:
#include
bool isValid(string s1, string s2){ if(s1.length() == s2.length()){ for(int i = 0; i < s1.length();i++){ s2[i]=tolower(s2[i]); s1[i]=tolower(s1[i]); if (s2[i] == ' '){ s2[i] = '_'; } if (s1[i] == ' '){ s1[i] = '_'; } } } if(s1.length() != s2.length()){ return false; } std::sort(s1.begin(), s1.end()); std::sort(s2.begin(), s2.end()); bool is_valid = true; for(int i = 0; i < s1.length();i++){ if(s1[i] != s2[i]){ is_valid = false; } } return is_valid; }
Vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
