fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cassert>
  4. #include <climits>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. bool isPermutation(std::string a, std::string b);
  9.  
  10. int main() {
  11. string a;
  12. a.push_back(0xE0);
  13. a.push_back(0xA0);
  14. a.push_back(0xA2);
  15. // string should hold U+0822, SAMARITAN VOWEL SIGN LONG A
  16.  
  17. string b;
  18. b.push_back(0xE0);
  19. b.push_back(0xA2);
  20. b.push_back(0xA0);
  21. // string should hold U+08A0, ARABIC LETTER BEH WITH SMALL V BELOW
  22.  
  23. cout << a << '\n' << b << '\n';
  24.  
  25. cout << boolalpha << isPermutation(a, b) << '\n'; // Outputs true which is wrong!
  26.  
  27. cout << boolalpha << is_permutation(a.begin(), a.end(), b.begin(), b.end()) << '\n'; // Outputs true which is wrong!
  28.  
  29. string aa(a); sort(aa.begin(), aa.end());
  30. string bb(b); sort(bb.begin(), bb.end());
  31. cout << boolalpha << (aa == bb) << '\n'; // Outputs true which is wrong!
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37. bool isPermutation(std::string a, std::string b)
  38. {
  39. if(a.length() != b.length())
  40. return false;
  41.  
  42. assert(a.length() <= INT_MAX);
  43. assert(b.length() <= INT_MAX);
  44.  
  45. int counts[256] = {};
  46. for (unsigned char ch : a)
  47. ++counts[ch];
  48. for (unsigned char ch : b)
  49. --counts[ch];
  50. for (int count : counts)
  51. if (count)
  52. return false;
  53.  
  54. return true;
  55. }
  56.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
ࠢ
ࢠ
true
true
true