What is the Problem with this code? When we write KKKK the problem is clear then. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the Problem with this code? When we write KKKK the problem is clear then.

//FOP LAB //Jawaad Asim //MTS 41 B //Lab 11 //Task 6 #include <iostream> #include <string> using namespace std; void permutation(string prefix, string str); void do_the_permutation (string permuted_word) { permutation("", permuted_word); } int main () { cout << "\n Q: Perform 4-letter WORD UNSCRAMBLING i.e. List all possible combinations of\n 4-letters in a word. Ex: The word 'TEST' can be unscrambled as TEST,TETS,TSET,\n TSTE,TTSE,TTES,etc?\n"; string word; cout << "\n\tPlease enter a word: "; cin >> word; cout << " "; do_the_permutation(word); cout << "\n\n\t"; system("pause"); return 0; } void permutation(string prefix, string str) { int n = str.length(); if (n == 0) { cout << prefix << "\t "; } else { for (int i = 0; i < n; i++) { permutation(prefix + str.at(i), str.substr(0, i) + str.substr(i+1, n)); } } } In this code when all letters are same we get wrong out put or if any of input is same we also get wrong output. can anyone help me?

14th Jun 2020, 9:42 AM
Ja Asim
Ja Asim - avatar
1 Answer
0
It is working fine. With duplicate letters, you get duplicate words. For example: aaa (012) aaa (021) aaa (102) aaa (120) aaa (201) aaa (210). The digits show the original string index. If you are looking for unique only, you must display and save the first occurence and ignore the second or beyond.
15th Jun 2020, 1:56 AM
John Wells
John Wells - avatar