Python Program for Generating Lyndon Words of Length n


In this problem, we will find all Lyndon words using the array's alphanumeric characters.

Before we start, let’s understand the definition of the Lyndon word.

  • All words are Lyndon words which are strictly lexicographically smaller than all of their rotations.

Here are examples of Lyndon words.

  • ab − The ‘ab’ is strictly lexicographically smaller than all of its permutations which is ‘ba’.

  • 89 − The rotations of ‘89’ is ‘98’, which is strictly lexicographically larger than ‘89’.

  • abc − The rotations of ‘abc’ are ‘bca’ and ‘cab’, which are strictly greater than ‘abc’.

Here are examples of non−Lyndon words.

  • aaa − The aaa is the non−Lyndon word, as all rotations of ‘aaa’ is the same.

  • bca − The ‘bca’ is non−Lyndon words as ‘abc’ its rotation is smaller than it,

Problem statement − We have given a chars array of length K containing the alphanumeric characters. Also, we have given n containing the positive integer. The task is that we need to find all Lyndon words of length n using the alphanumeric characters given in the array.

Sample examples

Input

chars = ['1', '3', '2'], n = 3

Output

112, 113, 122, 123, 132, 133, 223, 233

Explanation − It has generated all Lydon words of length 3 using the array characters.

Input

 n = 2, chars = ['1', '0']

Output

01

Explanation − The ‘01’ is the only Lyndon word that we can make using 0 and 1.

Input

 n = 2, chars = ['c', 'a', 'd']

Output

ac, ad, cd

Explanation − It has generated Lyndon words of length 2 using the a, c, and d characters.

Approach 1

We have a special algorithm to generate the Lyndon words called Duval’s algorithm.

Algorithm

Step 1 − Define the value of ‘n’ representing the Lyndon word’s length and chars array containing the characters to use while creating Lyndon words.

Step 2 − Sort the list.

Step 3 − Initialize the ‘indexes’ list with −1.

Step 4 − Make iterations until the indexes list is not null.

Step 5 − Increase the last element of the ‘indexes’ list by 1.

Step 6 − If list_size is equal to n, print the list values.

Step 7 − Append indexes to the list to make its length equal to n.

Step 8 − If the last element of the list is equal to the last index of the array, remove it from the list.

Example

Let’s understand the example with sample input.

  • The sorted list will be [‘a’, ‘c’, ‘d’].

  • The indexes list will be updated from [−1] to [0] in the first iteration. After that, it will equal the length of indexes to 2, and it will become [0, 0].

  • In the second iteration, the list will be updated to [0, 1], and we found the first Lyndon word, ' ac’.

  • The list will become [0, 2] in the third iteration, and the second Lyndon word is ‘ad’. Also, remove the last element from the list as it is equal to array_len −1.

  • In the fourth iteration, the list will become [1]. After that, it will be updated [1, 1].

  • The list will become [1, 2] in the next iteration,, and we found the third Lyndon wor, ' ‘cd’.

# Input
n = 2
chars = ['c', 'a', 'd']

# sort the list
initial_size = len(chars)
chars.sort()

# Initializing the list
indexes = [-1]

print("The Lyndon words of length {} is".format(n))

# Making iterations
while indexes:
    # Add 1 to the last element of the list
    indexes[-1] += 1
    list_size = len(indexes)

# If the list contains n characters, it means we found a Lyndon word
    if list_size == n:
        print(''.join(chars[p] for p in indexes))

    # Make the list size equal to n by adding characters
    while len(indexes) < n:
        indexes.append(indexes[-list_size])

    while indexes and indexes[-1] == initial_size - 1:
        indexes.pop()

Output

The Lyndon words of length 2 is
ac
ad
cd

Time complexity− O(nlogn), as we need to sort the ‘chars’ list initially.

Space complexity − O(n), as we store n indexes in the list.

Duval’s algorithm is the most effective approach to generate Lyndon words of length n. However, we have customized the approach to use only array characters.

Updated on: 14-Aug-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements