Skip to content

Commit 61bc960

Browse files
authored
Added valid anagram solution
1 parent 179d386 commit 61bc960

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ValidAnagram/solution.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <unordered_map>
2+
3+
class Solution {
4+
public:
5+
bool isAnagram(string s, string t)
6+
{
7+
// Use hashmap to keep count of letters in both strings
8+
unordered_map<char, int> s1LetterCount;
9+
unordered_map<char, int> s2LetterCount;
10+
11+
for (auto letter : s)
12+
s1LetterCount[letter]++;
13+
14+
for (auto letter : t)
15+
s2LetterCount[letter]++;
16+
17+
// The letter counts in each hashmap should match for the strings to be a valid anagram
18+
return (s1LetterCount == s2LetterCount);
19+
}
20+
};

0 commit comments

Comments
 (0)