Skip to content

Instantly share code, notes, and snippets.

@u8989332
Created October 13, 2020 13:52
Show Gist options
  • Save u8989332/5076d945a485e35327948280e555a536 to your computer and use it in GitHub Desktop.
Save u8989332/5076d945a485e35327948280e555a536 to your computer and use it in GitHub Desktop.
LeetCode - 242 Valid Anagram
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> sCheck(26);
vector<int> tCheck(26);
for(int i = 0 ; i < s.length();++i){
sCheck[s[i] - 'a']++;
}
for(int i = 0 ; i < t.length();++i){
tCheck[t[i] - 'a']++;
}
bool isSame = true;
for(int i = 0 ; i < 26;++i){
if(sCheck[i] != tCheck[i]){
isSame = false;
break;
}
}
return isSame;
}
};
int main() {
Solution sol;
cout << sol.isAnagram("anagram", "nagaram") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment