11
1
Background
A Lyndon word is a non-empty string which is strictly lexicographically smaller than all its other rotations. It is possible to factor any string uniquely as the concatenation of Lyndon words such that these subwords are lexicographically non-increasing; your challenge is to do this as succinctly as possible.
Details
You should implement a function or program which enumerates the Lyndon word factorization of any printable ASCII string, in order, outputting the resultant substrings as an array or stream of some kind. Characters should be compared by their code points, and all standard input and output methods are allowed. As usual for code-golf, the shortest program in bytes wins.
Test Cases
'' []
'C' ['C']
'aaaaa' ['a', 'a', 'a', 'a', 'a']
'K| ' ['K|', ' ']
'abaca' ['abac', 'a']
'9_-$' ['9_', '-', '$']
'P&O(;' ['P', '&O(;']
'xhya{Wd$' ['x', 'hy', 'a{', 'Wd', '$']
'j`M?LO!!Y' ['j', '`', 'M', '?LO', '!!Y']
'!9!TZ' ['!9!TZ']
'vMMe' ['v', 'MMe']
'b5A9A9<5{0' ['b', '5A9A9<5{', '0']
Related – xnor – 2017-06-19T20:49:31.717
Note that this is equivalent to splitting by
<=
ness. (I have no idea how to express this better :| ) – CalculatorFeline – 2017-06-19T21:01:38.103Is this equivalent to repeatedly taking the first character and the prefix of all characters bigger than it? – xnor – 2017-06-19T21:07:48.097
@xnor No. 'abac' is a Lyndon word. – user1502040 – 2017-06-19T21:13:14.107
@user1502040 I see, ties are interesting. I'd suggest adding some test cases that catch this. – xnor – 2017-06-19T21:14:20.137
@xnor Your proposed method can fail even when all characters are distinct. e.g. take the 'P&O(;' test case. – user1502040 – 2017-06-19T21:55:56.063
@user1502040 I think that one works. You first take the
P
, and the next character is smaller, so you just takeP
. Then, you take&
, and the rest of the string is bigger than it, so you take all of&O(;
. – xnor – 2017-06-19T23:08:54.380@xnor Ah right, brain failure. I was thinking of monotonic substrings rather than just > the first character. – user1502040 – 2017-06-19T23:19:35.180