I'm doing Leetcode 242. Valid Anagram and here's the 2 basic functions I came up with:
bool isAnagram1(string s, string t) {
vector<int> countS(26,0), countT(26,0);
for(char c : s){
countS[c - 'a']++;
}
for(char c : t){
countT[c - 'a']++;
}
return countS == countT;
}
bool isAnagram2(string s, string t) {
int countS[26], countT[26];
for(char c : s){
countS[c - 'a']++;
}
for(char c : t){
countT[c - 'a']++;
}
return countS == countT;
}
Basically, I'm wondering why the first solution works but the second one returns a wrong answer. In my mind, these were pretty much the same. I coded the first one after the second one wasn't working. I get no compiler errors. Maybe I'm misunderstanding something about arrays vs vector or the second solution is just plain wrong.