fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isAnagram(string s, string t) {
  5. if(s.length() != t.length()) return false;
  6.  
  7. unordered_map<char, int> counts;
  8.  
  9. for (int i = 0; i < s.length(); i++){
  10. counts[s[i]]++;
  11. counts[t[i]]--;
  12. }
  13.  
  14. for (auto &i : counts){
  15. if(i.second != 0) return false;
  16. }
  17.  
  18. return true;
  19. }
  20.  
  21. int main() {
  22. // your code goes here
  23. string s = "anagram", t = "nagaram";
  24. cout << isAnagram(s, t);
  25. return 0;
  26. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
1