# Permutations of Strings in C – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Permutations of Strings in C-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

`Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.`

C is one of the most widely used Programming Languages. it is basically used to build Operating System. C was developed by Dennis Ritchie in 1972. Below are some examples of C Programming which might you understanding the basics of C Programming.

#### Objective

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.
For example, s = [ab, ab, bc]. Only one instance of a permutation where all elements match should be printed. In other words, if s==s , then print either s s or s s 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 s = ab and a = ab 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 n, the length of the string array s.
Each of the next n lines contains a string s[i].

### Constraints :

• 2<=n<=9
• a<=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.

```2
ab
cd
```

```ab cd
cd ab
```

```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.

`Permutations of Strings in C – Hacker Rank Solution`
```#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;
}
``` #### Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker. 