Check Lyndon word

A Lyndon word is a string that is strictly lexicographically smaller than any of its cyclic rotations. Given a binary string, determine if it's a Lyndon word in as few bytes as possible.

For example, 001011 is a Lyndon word. Its rotations, listed below, are obtained by repeatedly moving the first symbol to the end.

001011
010110
101100
011001
110010
100101

Of these, the original string comes lexicographically first, or equivalently, represents the smallest binary number.

However, 001001 is not a Lyndon word because one of its rotations is the same as itself, which ties it for lexicographically earliest.

A related problem.

Input: A non-empty binary string or list of digits 0 and 1. You may not use numbers, like 5 to represent 101.

Output: A consistent Truthy or Falsey value that says whether the string is a Lyndon word.

Built-ins specifically for Lyndon words are not allowed.

Test cases:

The Lyndon words with length up to 6 are:

0
1
01
001
011
0001
0011
0111
00001
00011
00101
00111
01011
01111
000001
000011
000101
000111
001011
001101
001111
010111
011111

The non-Lyndon words of length up to 4 are:

00
10
11
000
010
100
101
110
111
0000
0010
0100
0101
0110
1000
1001
1010
1011
1100
1101
1110
1111

Python 2, 42

It seems to be good enough to compare with suffixes instead of bothering with a rotation.

f=lambda s,i=1:i/len(s)or s<s[i:]*f(s,i+1)

The setup of the recursion does not seem very nice; maybe it could be done better.

This 44-byte version makes it more obvious what's going on:

lambda s:all(s<=s[i:]for i in range(len(s)))

f x=all(x<=)$init$scanl(const.tail)x x

scanl(const.tail)x x builds a list of all suffixes of x, including the empty string "" at the end, which is stripped off with init.

Edit: @feersum spotted a bug in my first version and came up with the idea that comparing to suffixes is enough.

• How does it check that there aren't any rotations of x that are equal to x? – feersum Nov 6 '15 at 4:18
• @feersum: it doesn't. It's a bug. Fixed it. Thanks for finding out! – nimi Nov 6 '15 at 4:37

Pyth, 9 bytes

!f>z>zTUz

Demonstration

Uses Vihan et. al.'s suffixes approach.

• Dang it, I thought I was on to something there :p +1 – Downgoat Nov 6 '15 at 5:44

ShapeScript, 57 47 bytes

0?1'@"2"@+"20"$""~"21"$""~@2?2?>1<*'3?_1-*!@#@#

I created ShapeScript for this competition. The interpreter on GitHub has a slightly modified syntax.

Try it online!

• Trivia: This is my 700th PPCG answer. – Dennis Nov 7 '15 at 14:41

CJam, 15 14 bytes

r_,,\fm<(f>1-!

Try this fiddle in the CJam interpreter or verify all test cases at once.

How it works

r              e# Read a token from STDIN.
_,            e# Push the length of a copy.
,           e# Turn length L into [0 ... L-1].
\fm<       e# Rotate the token 0, ..., and L-1 units to the left.
(      e# Shift out the first rotation, i.e., the original token.
f>    e# Compare all other rotations with this one.
1-  e# Remove 1 from the resulting array of Booleans.
! e# Apply logical NOT to turn an empty array into 1, and a
e# non-empty one into 0.

J, 11 char

Outputs 1 on Lyndon words and 0 otherwise.

0=0({/:)<\.

<\. takes suffixes and then /: tells us how to sort them lexicographically. { takes the entry at the 0-th index and 0= checks if it's zero: if it is, we have a Lyndon word, because the biggest suffix wouldn't change place in a sort; if it's nonzero, it is not a Lyndon word, because some suffix is lexicographically earlier.

0=0({/:)<\. '001011'
1
0=0({/:)<\. '001001'
0

TeaScript, 10 bytes

Very golf, much short. Try it online

Explanation && Ungolfed

xe(#x<=xS(i))

xe(#      // Loop through x
// Check if all iterations return true
x <=  // Input is less than or equal to...
xS(i) // Input chopped at current index
)
• Holy cow, you're beating <s>Pyth</s> Dennis! How is this even possible?! – ETHproductions Nov 6 '15 at 4:40
• @ETHproductions In a world where Dennis can be out-golfed anything is possible :p – Downgoat Nov 6 '15 at 4:41
• I'll savor this moment while it lasts, then the CJam and Pyth answers will probably be golfed more – Downgoat Nov 6 '15 at 4:43
• Wait, wait... I see that this properly handles cases like 00, but how does it do this without catching on itself being equal to itself (i.e. when i==0)? – ETHproductions Nov 6 '15 at 4:46
• @ETHproductions This doesn't actually cycle much like feersum's answer, simply comparing the suffixes is functionally equivalent – Downgoat Nov 6 '15 at 4:47

f s=all(s<=)$init$scanr(:)[]s

Checks whether s is at most each of its non-empty suffixes, like nimi's answer.

The expression scanr(:)[] generates the list of suffixes by listing.

>> scanr(:)[] "abcd"
["abcd","bcd","cd","d",""]

The init then gets rid of the empty string at the end. Finally, all(s<=) checks whether every suffix x satisfies s<=x. Since the first suffix is s itself, a <= is needed.

Ruby, 37 bytes

->s{(1...s.size).all?{|i|s[i..-1]>s}}

Testing:

lyndon_words = %w(0 1 01 001 011 0001 0011 0111 00001 00011 00101 00111
01011 01111 000001 000011 000101 000111 001011 001101
001111 010111 011111)

not_lyndon_words = %w(00 10 11 000 010 100 101 110 111 0000 0010 0100 0101
0110 1000 1001 1010 1011 1100 1101 1110 1111)

f=->s{(1...s.size).all?{|i|s[i..-1]>s}}

p lyndon_words.all? &f      # => true
p not_lyndon_words.any? &f  # => false

Burlesque, 15 bytes

JiRJU_j<]x/==&&

Mainly 8 of those 7 bytes are to check if it doesn't tie. Otherwise you can go with simply JiR<]==.

Explanation:

J       -- duplicate word
iR      -- all rotations
J       -- duplicate list of all rotations
U_      -- check if list contains no duplicates
j       -- swap
<]      -- find minimum of the list
x/      -- rotate top
==      -- compare minimum with the original word
&&      -- and results of min == orig and list unique

Pyth - 12 bytes

Hope to golf a little bit.

.A<Lzt.<Lzlz

Javascript (ES6), 129 bytes

a=Array;s=prompt();console.log(a.from(a(s.length),(x,i)=>i).map(n=>(s.substring(n)+s.substring(0,n--))).sort().pop().contains(s))

Javascript, 91 87 bytes

f=x=>(y=(x+x).slice(1,-1),x[0]==x||!(y.indexOf(x)+1)&&!x.indexOf('0')&&x.slice(-1)==1);

I'm basically concatenating the word with itself and check if it's still there. To check if it is the smallest number possible, I just check that it start with a 0 and end with a 1.

Tests

[
['0',1],
['1',1],
['01',1],
['001',1],
['011',1],
['0001',1],
['0011',1],
['0111',1],
['00001',1],
['00011',1],
['00101',1],
['00111',1],
['01011',1],
['01111',1],
['000001',1],
['000011',1],
['000101',1],
['000111',1],
['001011',1],
['001101',1],
['001111',1],
['010111',1],
['011111',1],
['00',0],
['10',0],
['11',0],
['000',0],
['010',0],
['100',0],
['101',0],
['110',0],
['111',0],
['0000',0],
['0010',0],
['0100',0],
['0101',0],
['0110',0],
['1000',0],
['1001',0],
['1010',0],
['1011',0],
['1100',0],
['1101',0],
['1110',0],
['1111',0]
].forEach(t =>{
r=f(t[0])
x=t[1]
console.log('Test '+(r==x?'OK':'Fail (Expected: ' + x +')')
+'\nInput: '+t[0]+'\nResult: ' +r+'\n')
})

Mathematica, 86 bytes

(s=Table[#~StringRotateLeft~i,{i,StringLength@#}];Last@s==First@Sort@s&&s~Count~#==1)&

input

["1111"]