forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.4.cpp
39 lines (35 loc) · 863 Bytes
/
1.4.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
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool isAnagram1(string s, string t){
if(s=="" || t=="") return false;
if(s.length() != t.length()) return false;
sort(&s[0], &s[0]+s.length());
sort(&t[0], &t[0]+t.length());
if(s == t) return true;
else return false;
}
bool isAnagram(string s, string t){
if(s=="" || t=="") return false;
if(s.length() != t.length()) return false;
int len = s.length();
int c[256];
memset(c, 0, sizeof(c));
for(int i=0; i<len; ++i){
++c[(int)s[i]];
--c[(int)t[i]];
}
for(int i=0; i<256; ++i)
if(c[i] != 0)
return false;
return true;
}
int main()
{
string s = "aaabbb";
string t = "ababab";
//cout<<isAnagram(s, t)<<endl;
cout<<isAnagram(s, t)<<endl;
return 0;
}