Problem: Check if two strings are anagram.
Solution:
#include <iostream>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string input1 = "";
string input2 = "";
getline(cin, input1);
getline(cin, input2);
int size1 = input1.size();
int size2 = input2.size();
if (size1 != size2)
cout << "Not anagrams!";
else if (size1 == 0)
cout << "Anagrams!";
else {
map<char, int> map1, map2;
for (int i = 0; i < size1; i++)
map1[input1[i]]++;
for (int i = 0; i < size1; i++)
map2[input2[i]]++;
if (map1.size() == map2.size() &&
equal(map1.begin(), map1.end(), map2.begin()))
cout << "Anagrams!";
else
cout << "Not anagrams!";
}
return 0;
}
Solution:
#include <iostream>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string input1 = "";
string input2 = "";
getline(cin, input1);
getline(cin, input2);
int size1 = input1.size();
int size2 = input2.size();
if (size1 != size2)
cout << "Not anagrams!";
else if (size1 == 0)
cout << "Anagrams!";
else {
map<char, int> map1, map2;
for (int i = 0; i < size1; i++)
map1[input1[i]]++;
for (int i = 0; i < size1; i++)
map2[input2[i]]++;
if (map1.size() == map2.size() &&
equal(map1.begin(), map1.end(), map2.begin()))
cout << "Anagrams!";
else
cout << "Not anagrams!";
}
return 0;
}
No comments:
Post a Comment