fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. using namespace std;
  5.  
  6. bool IsUnique (const string & str) {
  7. int existing_chars[8] = {0,0,0,0,0,0,0,0};
  8. for(auto c : str) {
  9. if(existing_chars[c/32]&(1<<(c%32))) {
  10. return false;
  11. } else {
  12. existing_chars[c/32] |= 1<<(c%32) ;
  13. }
  14. }
  15. return true;
  16. }
  17.  
  18. bool IsPermutation (const string& str1, const string str2) {
  19. unordered_map<char,int> char_map;
  20. if(str1.length() != str2.length()) {
  21. return false;
  22. }
  23. for(auto c : str1) {
  24. char_map[c]++;
  25. }
  26. for(auto c : str2) {
  27. char_map[c]--;
  28. if(char_map[c] < 0) {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34.  
  35. int main() {
  36. cout<< IsPermutation("","") << IsPermutation("m","mm") << IsPermutation("mum","mmu") << IsPermutation("hello","olleh") << IsPermutation("abcdefghijk","kjhifgcabed");
  37. return 0;
  38. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10111