We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3d600b9 commit fb4a67bCopy full SHA for fb4a67b
ValidAnagram/solution.cpp
@@ -7,11 +7,14 @@ class Solution {
7
// Use hashmap to keep count of letters in both strings
8
unordered_map<char, int> letterCount;
9
10
- for (auto letter : s)
11
- letterCount[letter]++;
+ if (s.length() != t.length())
+ return false;
12
13
- for (auto letter : t)
14
- letterCount[letter]--;
+ for (int i=0; i<s.length(); i++)
+ {
15
+ letterCount[s.at(i)]++;
16
+ letterCount[t.at(i)]--;
17
+ }
18
19
// To be a valid anagram the letter counts in the hashmap should be all zeros if the count was the same in both strings
20
for (const auto& [letter, count]: letterCount)
0 commit comments