-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Anagram.cpp
More file actions
39 lines (28 loc) · 1015 Bytes
/
Copy pathValid Anagram.cpp
File metadata and controls
39 lines (28 loc) · 1015 Bytes
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
// Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
// An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
// Example 1:
// Input: s = "racecar", t = "carrace"
// Output: true
// Example 2:
// Input: s = "jar", t = "jam"
// Output: false
// Constraints:
// s and t consist of lowercase English letters.
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.length() == t.length()) {
unordered_map <char, int> CountS;
unordered_map <char, int> CountT;
for (int i = 0; i < s.length(); i++) {
CountS[s[i]]++;
CountT[t[i]]++;
}
// return true if they are anagram
return CountS == CountT;
} else {
return false;
std::cout << "cannot be anagrams if length differs :)" <<endl;
}
}
};