15. Valid Anagram
01 Jul 2020 | Daily Algorithms
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size())
{
return false;
}
unordered_map<char, int> mp;
int check=0;
for(auto& c : s)
{
mp[c]++;
}
for(auto& c : t)
{
if(mp[c]>=1)
{
mp[c]--;
check++;
}
}
cout<<check<<", "<< s.size();
if(check==s.size())
{
return true;
}
else
{
return false;
}
}
};
- store counts of every character into the hash table, second time, find the first character that appears
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size())
{
return false;
}
unordered_map<char, int> mp;
int check=0;
for(auto& c : s)
{
mp[c]++;
}
for(auto& c : t)
{
if(mp[c]>=1)
{
mp[c]--;
check++;
}
}
cout<<check<<", "<< s.size();
if(check==s.size())
{
return true;
}
else
{
return false;
}
}
};
- store counts of every character into the hash table, second time, find the first character that appears
Comments