forked from dhara04/cracking-the-coding-interview-c--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisanagram.cpp
29 lines (26 loc) · 776 Bytes
/
isanagram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Anagram needs to be a word in the dictionary hence the follwing code needs to be changed
//there are 2 ways to solve the problem
//1 sort the strings and compare them or calculate the count of each character and compare those counts
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
bool isanagram(string str1, string str2)
{
if (str1.empty()||str2.empty()){
cout<<"one of the string or both the strings is empty";
return false;
}
sort(str1.begin(),str1.end());
sort(str2.begin(),str2.end());
if ( str1 == str2 ) return true;
else return false;
}
int main()
{
bool sln = isanagram("cat", "actr");
if(sln) cout<<"is an anagram\n"; else cout<<"isn't an anagram\n";
/*isanagram("", "hello");
isanagram("", "");*/
return 0;
}