pastebin

Paste Search Dynamic
Recent pastes
areAnagrams
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool areAnagrams(string str1, string str2) {
  5.     if(str1.length() != str2.length()) {
  6.         return false;
  7.     }
  8.  
  9.     int freq[26] = {0};
  10.  
  11.     for(int i = 0; i < str1.length(); i++) {
  12.         freq[str1[i] - 'a']++;
  13.         freq[str2[i] - 'a']--;
  14.     }
  15.  
  16.     for(int i = 0; i < 26; i++) {
  17.         if(freq[i] != 0) {
  18.             return false;
  19.         }
  20.     }
  21.  
  22.     return true;
  23. }
  24.  
  25. int main() {
  26.         int t;
  27.         cin>>t;
  28.         while(t--){
  29.     string str1, str2;
  30.     cin >> str1 >> str2;
  31.  
  32.     if(areAnagrams(str1, str2)) {
  33.         cout << "True" << endl;
  34.     } else {
  35.         cout << "False" << endl;
  36.     }
  37. }
  38.     return 0;
  39. }
  40.  
Parsed in 0.012 seconds