Hackerrank solution - Permutations of Strings

Hackerrank solution - Permutations of Strings:

Welcome back Guys!!
In this post we will solve Permutation of strings Hackerrank problem.
It is a medium level problem using concept of  Character Array and String functions
from string.h library ...


The problem statement to Permutations of Strings hackerrank problem is as follows:


Strings are usually ordered in lexicographical order. That means they are ordered by comparing their leftmost different characters. For example,  because . Also  because . If one string is an exact prefix of the other it is lexicographically smaller, e.g., .
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, . 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 .
For example, . Only one instance of a permutation where all elements match should be printed. In other words, if , then print either  or  but not both.
A three element array having three discrete elements has six permutations as shown above. In this case, there are three matching pairs of permutations where  and  are switched. We only print the three visibly unique permutations:
ab ab bc
ab bc ab
bc ab ab
Input Format
The first line of each test file contains a single integer , the length of the string array .
Each of the next  lines contains a string .
Constraints
  •  contains only lowercase English letters.
Output Format
Print each permutation as a list of space-separated strings on a single line.
Sample Input 0
2
ab
cd
Sample Output 0
ab cd
cd ab
Sample Input 1
3
a
bc
bc
Sample Output 1
a bc bc
bc a bc
bc bc a

Explanation 1
This is similar to the note above. Only three of the six permutations are printed to avoid redundancy in output.

The solution code for Permutations of Strings hackerrank problem is as follows:


Generating the permutations in lexicographical order is a well-known problem with solution described here. The hardest part could be comparing the strings using C functions. In order to do that, we have function .
That said, string  is considered to be less than  if and only if . Notice that if  then . So in swap function we just swap the smaller one with bigger one in terms of lexiographic order.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char **s, int i, int j){
    char * tmp = s[i];
    s[i] = s[j];
    s[j] = tmp;
}

void reverse(char **s, int start, int end){
    while(start<end){
        swap(s,start++,end--);
    }
}

int next_permutation(int n, char **s)
{
    for(int i=n-2;i>-1;i--){
        if(strcmp(s[i+1],s[i])>0){
            //get min max
            for(int j=n-1;j>i;j--){
                if(strcmp(s[j],s[i])>0){
                    //do swap
                    swap(s,i,j);
                    // do reverse
                    reverse(s,i+1,n-1);
                    return 1;
                }
            }
        }
    }
    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(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;

}

Please read the code and analysis it.
Feel free to share your thoughts and doubts in the comment section below.
See you next time.

Post a Comment

3 Comments