Computer Science, asked by yuvichl5196, 8 months ago

Given the following permutation of a,b,c,d,e,f,g,h,i,j, what is the previous permutation in lexicographic (dictionary) order? Write your answer without any blank spaces between letters.

Answers

Answered by Anonymous
2

#include <bits/stdc++.h>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

{

int i, j;

for (i = 0; i < n-1; i++)

// Last i elements are already in place

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}

// Driver code

int main()

{

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

cout<<"Sorted array: \n";

printArray(arr, n);

return 0;

}

Answered by poojan
42

The next word is eibjfacdgh

Finding the next lexicographical word using python library functions. However, it is ideal for smaller lists only.

Language used : Python Programming

Program :  [Suitable for smaller lists]

from itertools import permutations

st=['a','b','c','d','e','f','g','h','i','j']

y=tuple('eibjdhgfca')  

#you can take any word into 'y' using input function, dynamically to find the next word.

st=permutations(list(st))

st=sorted(st)

ind=st.index(y)

print(''.join(st[ind+1]))

Output :

eibjfacdgh

Explanation :

  • Assign the letters needed to form the words with, as a list, into a variable.
  • Take a word whose next word we need to find in dictionary order, as directly (statically) into another variable. You can take it dynamically too from the user, using str(input()) function.
  • Using the permutations function, list all the permutations of the given list of letters and store the permutations list into a variable.
  • Sort the permutations list using sorted() that sorts the values into dictionary order.
  • Now find the index of the word taken whose next value we need to find.
  • Then access that word's index+1 th value from the list which will be the word we are looking for. That's it!

Learn more :

  • Printing all the palindromes formed by a palindrome word.

       brainly.in/question/19151384

  • Indentation is must in python. Know more about it at :

      brainly.in/question/17731168

Similar questions