← Back to challenges

Lexicographically First and Last

PythonHardstringssortingarrays

Instructions

Write a function that returns the lexicographically first and lexicographically last rearrangements of a lowercase string. Output the results in the following manner:

first_and_last(string) ➞ [first, last]

Examples

first_and_last("marmite") ➞ ["aeimmrt", "trmmiea"]

first_and_last("bench") ➞ ["bcehn", "nhecb"]

first_and_last("scoop") ➞ ["coops", "spooc"]

Notes

  • Lexicographically first: the permutation of the string that would appear first in the English dictionary (if the word existed).
  • Lexicographically last: the permutation of the string that would appear last in the English dictionary (if the word existed).
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Temperature Conversion