Skip to content

Commit

Permalink
C++ hashmap solution for valid anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitij98-Cpp committed Aug 3, 2022
1 parent 1f7b2f3 commit 8a243e2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cpp/neetcode_150/01_arrays_&_hashing/valid_anagram_hashmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// hashmap solution, similar to neetcode python implementation

class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size()) return false;

unordered_map<char,int> smap;
unordered_map<char,int> tmap;

for(int i = 0; i < s.size(); i++){
smap[s[i]]++;
tmap[t[i]]++;
}

for(int i = 0; i < smap.size(); i++){
if(smap[i] != tmap[i]) return false;
}
return true;
}
};

0 comments on commit 8a243e2

Please sign in to comment.