Finding a certain prefix of a string
Repetitions and periods in strings constitute one of the most fundamental
  areas of string combinatorics.1


The shortest prefix T of S where, 

$S = T^k.Q , s.t: k&gt;= 1,  Q =$ some prefix of T, then |T| is called the period of string.


  We say that string w has period p if w[i] = w[i + p] 1


Conventionally, finding T can be done by the Knuth–Morris–Pratt algorithm as well as Boyer–Moore string search algorithm.

Proving the correctness is beyond the scope of this answer.(More precisely, beyond my scope). See 1 for a better idea.



Before I came to know this, I attempted to solve this using KMP. And later, luckily, others have confirmed it. This could be helpful if one is wondering how could one possibly get an idea that KMP would work. I first tried to figure out the pattern and thought KMP could do the job.

Wondering how ? Our aim is to make $S$ a prefix of $T^n$ i.e. $TT \cdot .... \cdot T$, $n$ times. You can see that T is repeating itself, so, should some prefix of $S$ too. 

E.g. take $S = abc$, the only way you can have $TTT$ and $S$ as a prefix of $TTT$ ($T^3$) is if $T = abc$, then $abc$ is a prefix of $abcabcabc$. The minimum part which is repeating is the complete string $abc$. Hence, $T = abc$.

But, if $S = aba$, then $T$ can be $ab$, and $aba$ is a prefix of $TTT$ i.e. $ababab$. The minimum part which is repeating is just the string $ab$. Hence, $T = ab$.

You're roughly getting the idea that finding that part of prefix of $S$, where it is repeating itself in the rest of $S$ and ending with some part of it.

Final e.g. would be $S = abababa$, then again $T$ would remain $ab$, i.e, the prefix which is getting repeated and ending with some part of it. We could form $S$ as $T^k.Q: s.t. k &gt;= 1, Q =$ some prefix of $T$, here $a$. 

Now your job is pretty easy. For every prefix in $S$, check if it is present in the rest of $S$ as described above. The shortest one you find would be the answer.

KMP has an essential part called "Prefix Function". Below is the code for the Prefix function in C / C++. When I ran it over a couple of strings S, I found good results. 

void make_prefix_fun(char pat[], int a[], int m){
    // i is moving forward, and j is staying behind.
    a[0] = 0 ;
    for ( int i = 1, j = 0 ; i &lt; m ; i++ ){
        while ( pat[j] != pat[i] &amp;&amp; j &gt; 0 )
            j = a[j-1];
        if ( pat[j] == pat[i] )
            j++;
        a[i] = j;
    }
}


Sample Cases:
aba
a b a 
0 0 1 
//  ^ T = S[1..(|S| - 1)]

adbashdbhabsjd
a d b a s h d b h a b s j d 
0 0 0 1 0 0 0 0 0 1 0 0 0 0 
// T is the complete S    ^


abcdabcda
a b c d a b c d a 
0 0 0 0 1 2 3 4 5 
//              ^. T = S[1..(|S| - 5)]
// Cool. Found something here.


abababababababababa
a b a b a b a b a b a b a b a b a b a 
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
// Cool. Found something here.              ^. T = S[1..(|S| - 17)]

abababababababababaaaa
a b a b a b a b a b a b a b a b a b a a a a 
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 1 1 
// Working exactly as expected.                   ^


Found a pattern? Yes, we do.

If you observe the array, after the first couple of consecutive 0s, 


if we get the series, 1,2,3...x strictly upto the end, we can have our T = S[1..(|S| - x)] and the length of your required T is N - a[N] where N = |S|.
else T = entire string S.


The required T is the longest prefix of S, which is repeating itself in S, and S ends with some prefix of T or in other words T = S[1...|T|], where |T| is called the period of the string.



References:

1 - Repetitions in strings: algorithms and combinatorics, Crochemore et al.

Credits:

devnum, Mihai Calancea, Hendrik Jan.