Mathematica 124 bytes
x = StringLength@(y = "");
For[i = 1, ! (s = y~StringTake~i)~StringRepeat~x~StringContainsQ~y,i++];
First@Sort@StringPartition[s <> s, i, 1]
Whitespace and newlines (in the presence of semicolons at the ends of lines) have no meaning in Mathematica and are included here for readability.
Input goes in between the quotation marks in the first line. If recast as a function, that takes string input like so:
f=(x=StringLength@(y=#);For[i=1,!(s=y~StringTake~i)~StringRepeat~x~StringContainsQ~y,i++];First@Sort@StringPartition[s<>s,i,1])&
f@"bca"
(* "abc" *)
f@"abaa"
(* "aab" *)
then it's 128 bytes.
The For
loop takes the first i
characters of the input and repeats them at least up to the length of the input, then checks if the input is a substring of the result. Having found the length of the period of the string, the StringPartition
command concatenates two copies of that period and takes all substrings of that length from it (basically gets all cyclic permutations), then First@Sort
finds the first one of them when lexicographically ordered.
bac
in your example rather thanabc
? \$\endgroup\$bac
s. \$\endgroup\$(bca)^n
, which meansbca
is just as valid for the given example asabc
. \$\endgroup\$bca
is not the smallest lexicographically. \$\endgroup\$