File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -5,16 +5,21 @@ class Solution {
5
5
bool isAnagram (string s, string t)
6
6
{
7
7
// Use hashmap to keep count of letters in both strings
8
- unordered_map<char , int > s1LetterCount;
9
- unordered_map<char , int > s2LetterCount;
8
+ unordered_map<char , int > letterCount;
10
9
11
10
for (auto letter : s)
12
- s1LetterCount [letter]++;
11
+ letterCount [letter]++;
13
12
14
13
for (auto letter : t)
15
- s2LetterCount [letter]++ ;
14
+ letterCount [letter]-- ;
16
15
17
- // The letter counts in each hashmap should match for the strings to be a valid anagram
18
- return (s1LetterCount == s2LetterCount);
16
+ // To be a valid anagram the letter counts in the hashmap should be all zeros if the count was the same in both strings
17
+ for (const auto & [letter, count]: letterCount)
18
+ {
19
+ if (count != 0 )
20
+ return false ;
21
+ }
22
+
23
+ return true ;
19
24
}
20
25
};
You can’t perform that action at this time.
0 commit comments