Skip to content

Commit 342e4ab

Browse files
committed
solve: valid-anagram [week2]
1 parent 7cdbccc commit 342e4ab

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

valid-anagram/mkwkw.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
//set-up
1+
//map: key:alphabet, value:count
2+
//if t is an anagram of s
3+
// 1. same length
4+
// 2. same content of map
5+
class Solution {
6+
public:
7+
bool isAnagram(string s, string t) {
8+
9+
//1. different length -> false
10+
if(s.length()!=t.length())
11+
{
12+
return false;
13+
}
14+
15+
//2. different map -> false
16+
map<char, int> sMap, tMap;
17+
for(int i=0; i<s.length(); i++)
18+
{
19+
sMap[s[i]]++;
20+
tMap[t[i]]++;
21+
}
22+
23+
for(auto c : sMap)
24+
{
25+
if(tMap.find(c.first)==tMap.end())
26+
{
27+
return false;
28+
}
29+
if(tMap[c.first] != c.second)
30+
{
31+
return false;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
};

0 commit comments

Comments
 (0)