{"id":9549,"date":"2021-09-30T12:00:04","date_gmt":"2021-09-30T06:30:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=9549"},"modified":"2021-11-22T18:35:27","modified_gmt":"2021-11-22T13:05:27","slug":"python-program-to-print-all-permutations-of-a-string-in-lexicographic-order-without-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-all-permutations-of-a-string-in-lexicographic-order-without-recursion\/","title":{"rendered":"Python Program to Print All Permutations of a String in Lexicographic Order without Recursion"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n Strings are simple text in programming, which might be individual letters, words, phrases, or whole sentences. Python strings have powerful text-processing capabilities, such as searching and replacing characters, cutting characters, and changing case. Empty strings are written as two quotations separated by a space. Single or double quotation marks may be used. Three quote characters can be used to easily build multi-line strings.<\/p>\n Given a string ,the task is to print all the permutations of the given string in lexicographic order without using recursion in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Print All string permutations without recursion in lexicographic order:<\/p>\n Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output<\/a> & achieve your goal in improving java coding skills.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: Strings are simple text in programming, which might be individual letters, words, phrases, or whole sentences. Python strings have powerful text-processing capabilities, such as searching and replacing characters, cutting characters, and changing case. Empty strings are written as two quotations separated by a space. Single or double quotation marks may be used. …<\/p>\ngiven string ='hug'<\/pre>\n
printing all [ hug ] permutations :\r\nhug\r\nugh\r\nuhg\r\nghu\r\nguh\r\nhgu<\/pre>\n
given string ='tork'<\/pre>\n
printing all [ tork ] permutations :\r\ntork\r\ntrko\r\ntrok\r\nkort\r\nkotr\r\nkrot\r\nkrto\r\nktor\r\nktro\r\nokrt\r\noktr\r\norkt\r\nortk\r\notkr\r\notrk\r\nrkot\r\nrkto\r\nrokt\r\nrotk\r\nrtko\r\nrtok\r\ntkor\r\ntkro\r\ntokr<\/pre>\n
Program to Print All Permutations of a String in Lexicographic Order without Recursion<\/h2>\n
\n
1)Using for and while Loops(Static Input)<\/h3>\n
\n
from math import factorial\r\n\r\n\r\ndef printPermutationsLexico(string):\r\n # In lexicographic sequence, print all permutations of string s.\r\n stringseq = list(string)\r\n\r\n # There will be n! permutations where n = len (seq).\r\n for t in range(factorial(len(stringseq))):\r\n # print permutation by joining all the characters using join() function\r\n print(''.join(stringseq))\r\n\r\n # Find p such that seq[per:] is the longest sequence with lexicographic\r\n # elements in decreasing order.\r\n per = len(stringseq) - 1\r\n while per > 0 and stringseq[per - 1] > stringseq[per]:\r\n per -= 1\r\n\r\n # reverse the stringsequence from per to end using reversed function\r\n stringseq[per:] = reversed(stringseq[per:])\r\n\r\n if per > 0:\r\n # Find q such that seq[q] is the smallest element in seq[per:] and seq[q] is greater\r\n # than seq[p - 1].Find q such that seq[q] is the smallest\r\n # element in seq[per:] and seq[q] is greater than seq[per - 1].\r\n q = per\r\n while stringseq[per - 1] > stringseq[q]:\r\n q += 1\r\n\r\n # swapping seq[per - 1] and seq[q]\r\n stringseq[per - 1], stringseq[q] = stringseq[q], stringseq[per - 1]\r\n\r\n\r\n# given string as static input\r\ngiven_string = 'hug'\r\n# printing all the perumutations\r\nprint('printing all [', given_string, '] permutations :')\r\n# passing given string to printPermutationsLexico function\r\nprintPermutationsLexico(given_string)\r\n<\/pre>\n
printing all [ hug ] permutations :\r\nhug\r\nugh\r\nuhg\r\nghu\r\nguh\r\nhgu<\/pre>\n
\n
2)Using for and while Loops(User Input)<\/h3>\n
\n
from math import factorial\r\n\r\n\r\ndef printPermutationsLexico(string):\r\n # In lexicographic sequence, print all permutations of string s.\r\n stringseq = list(string)\r\n\r\n # There will be n! permutations where n = len (seq).\r\n for t in range(factorial(len(stringseq))):\r\n # print permutation by joining all the characters using join() function\r\n print(''.join(stringseq))\r\n\r\n # Find p such that seq[per:] is the longest sequence with lexicographic\r\n # elements in decreasing order.\r\n per = len(stringseq) - 1\r\n while per > 0 and stringseq[per - 1] > stringseq[per]:\r\n per -= 1\r\n\r\n # reverse the stringsequence from per to end using reversed function\r\n stringseq[per:] = reversed(stringseq[per:])\r\n\r\n if per > 0:\r\n # Find q such that seq[q] is the smallest element in seq[per:] and seq[q] is greater\r\n # than seq[p - 1].Find q such that seq[q] is the smallest\r\n # element in seq[per:] and seq[q] is greater than seq[per - 1].\r\n q = per\r\n while stringseq[per - 1] > stringseq[q]:\r\n q += 1\r\n\r\n # swapping seq[per - 1] and seq[q]\r\n stringseq[per - 1], stringseq[q] = stringseq[q], stringseq[per - 1]\r\n\r\n\r\n# Enter some random string as user input using int(input()) function.\r\ngiven_string =input('Enter some random string = ')\r\n# printing all the perumutations\r\nprint('printing all [', given_string, '] permutations :')\r\n# passing given string to printPermutationsLexico function\r\nprintPermutationsLexico(given_string)\r\n<\/pre>\n
Enter some random string = epic\r\nprinting all [ epic ] permutations :\r\nepic\r\nicep\r\nicpe\r\niecp\r\niepc\r\nipce\r\nipec\r\npcei\r\npcie\r\npeci\r\npeic\r\npice\r\npiec\r\nceip\r\ncepi\r\nciep\r\ncipe\r\ncpei\r\ncpie\r\necip\r\necpi\r\neicp\r\neipc\r\nepci<\/pre>\n
\n