how to find the smallest string from a string . in other words find the smallest string such that it can be concatenated some number of times to obtain big string.
Input rbrb output rb (my function is working fine)
Another example
Input bcdbcdbcdbcd output :bcd (my function working fine)
I tried like this.
function getSmallestString(s){
let i= 0;
let tem = '';
while(true){
let mid = s.length/2;
let tem = s.substring(0,mid);
if(tem + tem == s){
s= tem
}else {
return s;
}
}
}
console.log('bcdbcdbcdbcd')
https://jsbin.com/liracurala/1/edit?html,js,output
here my case fail
function getSmallestString(s){
let i= 0;
let tem = '';
while(true){
let mid = Math.floor(s.length/2);
let tem = s.substring(0,mid);
if(tem + tem == s){
s= tem
}else {
return s;
}
}
}
console.log(getSmallestString('ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'))
Expected output is o
Each answer fail in this case lrbb expected output lrbb