manasij7479

Anagram Check

Jun 9th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<map>
  4. bool check(std::vector<int> a,std::vector<int> b)
  5. {
  6.     if(a.size()!=b.size())return false;
  7.     std::map<int,int> fd;//using hashing here will decrease time complexity while increasing place required
  8.     for(int i=0;i<a.size();++i)
  9.     {
  10.         fd[a[i]]++;
  11.         fd[b[i]]--;
  12.     }
  13.     for(auto& x:fd)
  14.         if(x.second!=0)return false;
  15.     return true;
  16. }
  17. int main()
  18. {
  19.     std::cout<<check({1,2,3,4,5},{5,4,3,2,1});
  20. }
Advertisement
Add Comment
Please, Sign In to add comment