fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. bool shouldSwap(const string& str, size_t start, size_t index) {
  7. for (auto i = start; i < index; ++i) {
  8. if (str[i] == str[index])
  9. return false;
  10. }
  11. return true;
  12. }
  13.  
  14. void permute(string& str, size_t index)
  15. {
  16. if (index >= str.size()) {
  17. cout << str << endl;;
  18. return;
  19. }
  20.  
  21. for (size_t i = index; i < str.size(); ++i) {
  22. if(shouldSwap(str, index, i)) {
  23. swap(str[index], str[i]);
  24. permute(str, index + 1);
  25. swap(str[index], str[i]);
  26. }
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. string str = "0101";
  33. permute(str, 0);
  34. return 0;
  35. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0101
0110
0011
1001
1010
1100