Trying to grasp a javascript string variable source;
Is there an efficient technique to identify all REPEATED substrings of length around 20 characters (even if they overlap or include substrings of slightly different lengths)?
An approach that could be considered is:
var len=20;
var sourceLen=source.length;
for (var i=0; i<sourceLen; i++){
for (var j=i+1; j<sourceLen; j++){
if (source.substring(i,i+len)==source.substring(j,j+len)){
console.log(source.substring(i,i+len));
}
}
}
However, as the string size increases, the computation time also escalates significantly. There's been pondering about adjusting the steps of value exchanging (j+=5; instead of j++), but encountered certain constraints.
Another idea has been to explore using .indexOf
to achieve similar outcomes with just one for-loop.
Are there any smarter strategies available to extract a list of duplicated strings of approximately 20 characters within the given string?