Permutations of Strings C


Problem Statement :


Strings are usually ordered in lexicographical order. That means they are ordered by comparing their leftmost different characters. For example,  abc < abd  because  c < d. Also z > yyy because  z > y. If one string is an exact prefix of the other it is lexicographically smaller, e.g., gh < ghij.

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. See the 'note' below for an example.

Complete the function next_permutation which generates the permutations in the described order.

For example, s= [ab, bc, cd ] . The six permutations in correct order are:

ab bc cd
ab cd bc
bc ab cd
bc cd ab
cd ab bc
cd bc ab

Note: There may be two or more of the same string as elements of s. 

Input Format

   The first line of each test file contains a single integer n , the length of the string array s.
   Each of the next n lines contains a string s[i].


Constraints

    2  <=  n <= 9
    1  <=  s[i]  <= 10
    s[i]  contains only lowercase English letters.

Output Format

Print each permutation as a list of space-separated strings on a single line.



Solution :



title-img


                            Solution in C :

#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;
}
                        








View More Similar Problems

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →

Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

View Solution →

Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

View Solution →