gh >ghij<\/strong><\/em>.<\/p>\n\n\n\nGiven an array of strings sorted in lexicographical order, print all of its permutations in strict lexicographical order. If two permutations look the same, only print one of them. See the ‘note’ below for an example.<\/p>\n\n\n\n
Complete the function next_permutation<\/code> which generates the permutations in the described order.<\/p>\n\n\n\nFor example, s =<\/strong> [ab, bc, cd<\/em>]<\/strong>. The six permutations in correct order are:<\/p>\n\n\n\nab bc cd\nab cd bc\nbc ab cd\nbc cd ab\ncd ab bc\ncd bc ab<\/code><\/pre>\n\n\n\nNote:<\/strong> There may be two or more of the same string as elements of s<\/em><\/strong>.
For example, s<\/em> = [ab, ab, bc<\/em>]<\/strong>. Only one instance of a permutation where all elements match should be printed. In other words, if s<\/em>[0] == s<\/em>[1]<\/strong>, then print either s[0]<\/strong> s[1]<\/strong> or s[1]<\/strong> s[0]<\/strong> but not both.<\/p>\n\n\n\nA three element array having three distinct elements has six permutations as shown above. In this case, there are three matching pairs of permutations where s[0]<\/strong> = ab<\/strong><\/em> and s[1]<\/strong> = ab<\/strong> are switched. We only print the three visibly unique permutations:<\/p>\n\n\n\nab ab bc\nab bc ab\nbc ab ab<\/code><\/pre>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line of each test file contains a single integer n<\/strong><\/em>, the length of the string array s<\/strong><\/em>.<\/p>\n\n\n\nEach of the next n<\/em><\/strong> lines contains a string s[i]<\/em><\/strong>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 2 <= n<\/em> <= 9<\/strong><\/li>
- 1 <= |s[i]<\/em>| <= 10<\/strong><\/li>
- s[i] <\/em><\/strong>contains only lowercase English letters.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint each permutation as a list of space-separated strings on a single line.<\/p>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\n2\nab\ncd<\/code><\/pre>\n\n\n\nSample Output 0<\/strong><\/p>\n\n\n\nab cd\ncd ab<\/code><\/pre>\n\n\n\nSample Input 1<\/strong><\/p>\n\n\n\n3\na\nbc\nbc<\/code><\/pre>\n\n\n\nSample Output 1<\/strong><\/p>\n\n\n\na bc bc\nbc a bc\nbc bc a<\/pre>\n\n\n\nExplanation 1<\/strong><\/p>\n\n\n\nThis is similar to the note<\/strong> above. Only three of the six permutations are printed to avoid redundancy in output.<\/p>\n\n\n\n<\/span>Solution – Permutations of Strings in C<\/strong><\/span><\/h2>\n\n\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nvoid swap(char **s, int i, int j)\n{\n char * tmp = s[i];\n s[i] = s[j];\n s[j] = tmp;\n}\n\nvoid reverse(char **s, int start, int end)\n{\n while(start<end)\n {\n swap(s,start++,end--);\n }\n}\n\nint next_permutation(int n, char **s)\n{\n\t\/**\n\t* Complete this method\n\t* Return 0 when there is no next permutation and 1 otherwise\n\t* Modify array s to its next permutation\n\t*\/\n for(int i=n-2;i>-1;i--)\n {\n if(strcmp(s[i+1],s[i])>0)\n {\n \/\/get min max\n for(int j=n-1;j>i;j--)\n {\n if(strcmp(s[j],s[i])>0)\n {\n \/\/do swap\n swap(s,i,j);\n \/\/ do reverse\n reverse(s,i+1,n-1);\n return 1;\n }\n }\n }\n }\n return 0;\n}\n\n<\/pre>\n\n\n\n