In this HackerRank Permutations of Strings problem in C programming problem we have given 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.


HackerRank Permutations of Strings in c problem solution

HackerRank Permutations of Strings in c problem solution

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>

int next_permutation(int n, char **s)
{
   /**
   * Complete this method
   * Return 0 when there is no next permutation and 1 otherwise
   * Modify array s to its next permutation
   */
    for (int i = n - 1; i > 0; i--)
        if (strcmp(s[i], s[i - 1]) > 0)
        {
            int j = i + 1;
            for (; j < n; j++) if (strcmp(s[j], s[i - 1]) <= 0) break;
            char *t = s[i - 1];
            s[i - 1] = s[j - 1];
            s[j - 1] = t;
            for (; i < n - 1; i++, n--)
            {
                t = s[i];
                s[i] = s[n - 1];
                s[n - 1] = t;
            }
            return 1;
        }
    for (int i = 0; i < n - 1; i++, n--)
    {
        char *t = s[i];
        s[i] = s[n - 1];
        s[n - 1] = t;
    }
    return 0;
}

int main()
{
   char **s;
   int n;
   scanf("%d", &n);
   s = calloc(n, sizeof(char*));
   for (int i = 0; i < n; i++)
   {
      s[i] = calloc(n, sizeof(char) * 11);
      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;
}