c++:
class Solution {
public:
bool isAnagram(string s, string t) {
int record[26] = {0};
for(int i = 0; i < s.size(); i++){
record[s[i] - 'a']++;
}
for(int i = 0; i < t.size(); i++){
record[t[i]- 'a']--; }
for(int i = 0; i < 26; i++){
if(record[i] != 0){
return false;
}
}
return true;
}
};
go:
func isAnagram(s string, t string) bool {
if len(s) != len(t){
return false
}
exists := make(map[byte]int)
for i := 0; i < len(s); i++{
if v,ok := exists[s[i]];v > 0 && ok{
exists[s[i]] = v+1
}else{
exists[s[i]] = 1
}
}
for i := 0; i < len(t); i++{
if v,ok := exists[t[i]]; v > 0 && ok{
exists[t[i]] = v-1
}else{
return false
}
}
return true
}
func isAnagram(s string, t string) bool {
record := [26]int{}
for _,r := range s{
record[r - rune('a')]++
}
for _,r := range t{
record[r - rune('a')]--
}
return record == [26]int{}
}
|