Statistics

Problem Statement for "Permutation"

Problem Statement

A permutation of the letters in an alphabet can be described by a string that contains each letter exactly once. For example, CABD describes the permutation of ABCD that maps A to C, B to A, C to B, and D to D. If we repeatedly apply that permutation, we will get the sequence ABCD,CABD,BCAD,ABCD,CABD,BCAD,ABCD,... This permutation is cyclic with length 3.

We want to find the permutation of the first n letters of

    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
that has the longest cycle. Create a class Permutation that contains the method best that takes the number of characters n as input and returns the lexicographically first permutation that has the maximum possible cycle length.

"Lexicographically first" means the String that would sort first among all the permutations of maximum cycle length, using the ASCII sequence (which sorts uppercase letters before lowercase letters).

Definition

Class:
Permutation
Method:
best
Parameters:
int
Returns:
String
Method signature:
String best(int n)
(be sure your method is public)

Constraints

  • n is between 1 and 52 inclusive

Examples

  1. 6

    Returns: "ACBEFD"

    This permutation has cycle length 6. So does BDEFCA, but ACBEFD is lexicographically first. The cycle is: ABCDEF -> ACBEFD -> ABCFDE -> ACBDEF -> ABCEFD -> ACBFDE -> ABCDEF

  2. 7

    Returns: "BCAEFGD"

    This is the lexicographically first permutation with cycle length 12

  3. 29

    Returns: "BCDEAGHIJKLFNOPQRSTMVWXYZabcU"

  4. 1

    Returns: "A"

  5. 8

    Returns: "BCAEFGHD"

  6. 2

    Returns: "BA"

  7. 3

    Returns: "BCA"

  8. 4

    Returns: "BCDA"

  9. 5

    Returns: "BADEC"

  10. 9

    Returns: "BCDAFGHIE"

  11. 10

    Returns: "BADECGHIJF"

  12. 11

    Returns: "ACBEFDHIJKG"

  13. 12

    Returns: "BCAEFGDIJKLH"

  14. 13

    Returns: "ACDBFGHEJKLMI"

  15. 14

    Returns: "BCAEFGDIJKLMNH"

  16. 15

    Returns: "BCAEFGHDJKLMNOI"

  17. 16

    Returns: "BCDAFGHIEKLMNOPJ"

  18. 17

    Returns: "BADECGHIJFLMNOPQK"

  19. 18

    Returns: "ACBEFDHIJKGMNOPQRL"

  20. 19

    Returns: "BCAEFGDIJKLHNOPQRSM"

  21. 20

    Returns: "ACDBFGHEJKLMIOPQRSTN"

  22. 21

    Returns: "ABDECGHIFKLMNJPQRSTUO"

  23. 22

    Returns: "ABCEFDHIJGLMNOKQRSTUVP"

  24. 23

    Returns: "BCAEFGHDJKLMNOIQRSTUVWP"

  25. 24

    Returns: "ACDBFGHIEKLMNOPJRSTUVWXQ"

  26. 25

    Returns: "BCDAFGHIEKLMNOPJRSTUVWXYQ"

  27. 26

    Returns: "ACDEBGHIJFLMNOPQKSTUVWXYZR"

  28. 27

    Returns: "BCDAFGHIEKLMNOPJRSTUVWXYZaQ"

  29. 28

    Returns: "BADECGHIJFLMNOPQKSTUVWXYZabR"

  30. 30

    Returns: "BCAEFGDIJKLHNOPQRSMUVWXYZabcdT"

  31. 31

    Returns: "ACDBFGHEJKLMIOPQRSTNVWXYZabcdeU"

  32. 32

    Returns: "BCAEFGDIJKLHNOPQRSMUVWXYZabcdefT"

  33. 33

    Returns: "ACDBFGHEJKLMIOPQRSTNVWXYZabcdefgU"

  34. 34

    Returns: "BCAEFGHDJKLMNOIQRSTUVWPYZabcdefghX"

  35. 35

    Returns: "ACDBFGHIEKLMNOPJRSTUVWXQZabcdefghiY"

  36. 36

    Returns: "BCDAFGHIEKLMNOPJRSTUVWXYQabcdefghijZ"

  37. 37

    Returns: "ACDEBGHIJFLMNOPQKSTUVWXYZRbcdefghijka"

  38. 38

    Returns: "BCDAFGHIEKLMNOPJRSTUVWXYQabcdefghijklZ"

  39. 39

    Returns: "ACDEBGHIJFLMNOPQKSTUVWXYZRbcdefghijklma"

  40. 40

    Returns: "BCDEAGHIJKLFNOPQRSTMVWXYZabcUefghijklmnd"

  41. 41

    Returns: "BADECGHIJFLMNOPQKSTUVWXYZabRdefghijklmnoc"

  42. 42

    Returns: "BCDEAGHIJKLFNOPQRSTMVWXYZabcUefghijklmnopd"

  43. 43

    Returns: "BCAEFGDIJKLHNOPQRSMUVWXYZabcdTfghijklmnopqe"

  44. 44

    Returns: "ACDBFGHEJKLMIOPQRSTNVWXYZabcdeUghijklmnopqrf"

  45. 45

    Returns: "ABDECGHIFKLMNJPQRSTUOWXYZabcdefVhijklmnopqrsg"

  46. 46

    Returns: "ABCEFDHIJGLMNOKQRSTUVPXYZabcdefgWijklmnopqrsth"

  47. 47

    Returns: "BCAEFGHDJKLMNOIQRSTUVWPYZabcdefghXjklmnopqrstui"

  48. 48

    Returns: "ACDBFGHIEKLMNOPJRSTUVWXQZabcdefghiYklmnopqrstuvj"

  49. 49

    Returns: "BCDAFGHIEKLMNOPJRSTUVWXYQabcdefghijZlmnopqrstuvwk"

  50. 50

    Returns: "ACDEBGHIJFLMNOPQKSTUVWXYZRbcdefghijkamnopqrstuvwxl"

  51. 51

    Returns: "ABDEFCHIJKGMNOPQRLTUVWXYZaScdefghijklbnopqrstuvwxym"

  52. 52

    Returns: "ABCEFGDIJKLHNOPQRSMUVWXYZabTdefghijklmcopqrstuvwxyzn"

  53. 52

    Returns: "ABCEFGDIJKLHNOPQRSMUVWXYZabTdefghijklmcopqrstuvwxyzn"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: