Algorithm


Problem Name: Permutations of Strings

Problem Link: https://www.hackerrank.com/challenges/permutations-of-strings/problem?isFullScreen=true

In this HackerRank Functions in C programming problem solution,

trings 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 x > y.f 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, bc, cd]. Only one instance of a permutation where all elements match should be printed. In other words, if s [0] == s[1], then print either s[0] s[1] or s[1] but not both. A three element array having three distinct elements has six permutations as shown above. In this case, there are three matching pairs of permutations where s [0] = ab and s [1] = 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
  • 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.

    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
    

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#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
	*/
    int k, l;

  // 1. find the largest index k such that s[k]  <  s[k+1]

  for (k = n - 2; k >= 0; k --) {

    if(strcmp(s[k], s[k+1]) < 0) break;

  }
  if (k  <  0> return 0;

  // 2. find the largest index l greater than k such that s[k] < s[l]
  for (l = n -1; l > k; l --) {
    if(strcmp(s[k], s[l]) < 0) break;
  }
  // 3. swap elements present at k, l
  char * tmp = s[k];
  s[k] = s[l];
  s[l] = tmp;
 // 4. reverse the sequence of elements from k+1 to n
  for(int i = k + 1, j = n -1; i  <  j; i ++, j --> {
    tmp = s[i];
    s[i] = s[j];
    s[j] = tmp;
  }
  return 1;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
2 ab cd

Output

x
+
cmd
ab cd cd ab
Advertisements

Demonstration


Previous
[Solved] Dynamic Array in C in C solution in Hackerrank - Hacerrank solution C
Next
[Solved] Sorting Array of Strings in C in C solution in Hackerrank - Hacerrank solution C