-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.cpp
45 lines (32 loc) · 950 Bytes
/
anagram.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
40
41
42
43
44
45
/*
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
*/
class Solution {
public:
bool isAnagram(string s, string t) {
int length = s.length();
int length2 = t.length();
if(length != length2) {
return false;
}
unordered_map<char,int> myHash;
unordered_map<char,int> myHash2;
for(int i=0;i<length;i++) {
myHash[s[i]]++;
myHash2[t[i]]++;
}
for(int i=0;i<length;i++) {
if(myHash[s[i]] != myHash2[s[i]]) {
return false;
}
}
return true;
}
};