fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <assert.h>
  5. using namespace std;
  6.  
  7. template <class Char, class Trait>
  8. bool isAnagram(const basic_string<Char, Trait> & input1, const basic_string<Char, Trait> & input2) {
  9. typedef vector<Char> Vector;
  10. Vector data1(input1.begin(), input1.end()), data2(input2.begin(), input2.end());
  11. if (data1.size() != data2.size())
  12. return false;
  13. for(char c: data1) {
  14. bool found = false;
  15. for (typename Vector::iterator i = data2.begin(); i != data2.end(); i++) {
  16. if (Trait::eq(c, *i)) {
  17. data2.erase(i);
  18. found = true;
  19. break;
  20. }
  21. }
  22. if (!found)
  23. return false;
  24. }
  25. assert(data2.empty());
  26. return true;
  27. }
  28.  
  29. void check(string input1, string input2) {
  30. cout << input1 << "/" << input2 << " = " << (isAnagram(input1, input2) ? "true" : "false") << "\n";
  31. }
  32.  
  33. void check(wstring input1, wstring input2) {
  34. wcout << input1 << L"/" << input2 << L" = " << (isAnagram(input1, input2) ? L"true" : L"false") << L"\n";
  35. }
  36.  
  37. int main() {
  38. check("blah", "habl");
  39. check("heck", "eckl");
  40. check(L"русский", L"урсский");
  41. return 0;
  42. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
blah/habl = true
heck/eckl = false
@CAA:89/C@AA:89 = false