We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C
- Functions
- Permutations of Strings
- Discussions
Permutations of Strings
Permutations of Strings
Sort by
recency
|
137 Discussions
|
Please Login in order to post a comment
include
include
include
int next_permutation(int n, char **s) { int k = -1; for (int i = 0; i < n - 1; i++) { if (strcmp(s[i], s[i + 1]) < 0) { k = i; } } if (k == -1) { return 0; // No next permutation }
}
int main() { char **s; int n; scanf("%d", &n); s = calloc(n, sizeof(char*)); for (int i = 0; i < n; i++) { s[i] = calloc(11, sizeof(char)); scanf("%s", s[i]); } do { for (int i = 0; i < n; i++) printf("%s%c", s[i], i == n - 1 ? '\n' : ' '); } while (next_permutation(n, s)); for (int i = 0; i < n; i++) free(s[i]); free(s); return 0; }
Tried to make is as simple as possible
This is a horrible question.
I agree lol
Thanks but if size of array is large..I think better to start at the end:
it still worked
Refer here : https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
Thank you for the wikipedia link!!!