Upon the rumor that Codegolf will have a Rock-Paper-Scissors tournament you look into the topic of square-free words. A word made of the letters R
, P
, S
is square-free if it does not contain a sequence that repeats twice. That is to say, the word can not be written as
a x x b
where a
and b
are words of any length and x
is a word of length at least one, all made of the letters R
, P
, S
.
Task
Write a program that generates the square-free words of the letters R
, P
, S
of length n
where the number 1 <= n <= 10
is taken as input.
Example
For example the square-free words of length 3 are
RPR
, RSR
, RPS
, RSP
, SPS
, SRS
, SRP
, SPR
, PRP
, PSP
, PSR
, PRS
and those of length 4 are
RPRS
, RPSR
, RPSP
, RSRP
, RSPR
, RSPS
, PRPS
, PRSR
, PRSP
, PSRP
, PSRS
, PSPR
, SRPR
, SRPS
, SRSP
, SPRP
, SPRS
, SPSR
and note that for example SPSP
or PRPR
are not square-free
Rules
- This is codegolf, shortest program wins, standard loopholes are closed.
- You may print the words or create them in memory.
- Your program may be written as a function.
References
Wikipedia entry on square-free words
The number of square-free ternary words of given length are in https://oeis.org/A006156
n>3
would be a good idea, because there has been some confusion about repeated characters vs. repeated sequences. \$\endgroup\$