fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int N, M;
  9. cin >> N >> M; // Taking input for N and M
  10.  
  11. unordered_set<string> uniqueStrings; // Set to store unique N-length strings
  12.  
  13. // Taking input for N strings
  14. for (int i = 0; i < N; ++i) {
  15. string input;
  16. cin >> input;
  17.  
  18. // Iterate over each position (K) in the string
  19. for (int k = 0; k < M; ++k) {
  20. string newString = ""; // New string formed by taking Kth character from each input string
  21.  
  22. // For each position (K), create a new string
  23. for (int j = 0; j < N; ++j) {
  24. newString += input[j == i ? k : j];
  25. }
  26.  
  27. uniqueStrings.insert(newString); // Add the newly formed string to the set
  28. }
  29. }
  30.  
  31. // Output all strings from the set
  32. for (const string& str : uniqueStrings) {
  33. cout << str << endl;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5276KB
stdin
2 3
abc 
def
stdout
df
bb
ab
cb
dd
de