javascriptstringlongest-substring

The longest repeating substrings JavaScript


I have a programm that can find the longest repeating substring of entered string, but the thing is, when there are 2 repeating substrings of the biggest length in answer I get only one of them.

For instance, for the string 123b456k123m456j the answer is 123, while answer 123 and 456 is expected..

Can I somehow fix this problem? If you know how, please answer my question))

let s = prompt('Enter string');

   
function substr(str, begin, end) {
  let result = "";
  for (let i = begin; i < end; i++) {
    result += str[i];
  }
  return result;
}

function lcp(str1, str2) {
  let n = Math.min(str1.length, str2.length);
  for (let i = 0; i < n; i++) {
    if (str1.charAt(i) != str2.charAt(i)) {
      return substr(str1, 0, i);
    }
  }
  return substr(str1, 0, n);
}

function lrs(str) {
  const suffixes = [];
  for (let i = 0; i < str.length; i++) {
    suffixes.push(substr(str, i, str.length));
  }
 
  suffixes.sort();
 
  let result = '';
  let res=[];
  for (let i = 0; i < str.length - 1; i++) {
    const p = lcp(suffixes[i], suffixes[i + 1]);
    if (p.length > result.length){
     result = p;
    }
  }
  return result;
}

alert(lrs(s));


Solution

  • Try replacing your function Los with the below.

    function lrs(str) {
    const suffixes = [];
    for (let i = 0; i < str.length; i++) {
        suffixes.push(substr(str, i, str.length));
    }
    
    suffixes.sort();
    
    let result = [];
    let res = [];
    let lastLongestSubStrLen = 0;
    for (let i = 0; i < str.length - 1; i++) {
        const p = lcp(suffixes[i], suffixes[i + 1]);
        if (lastLongestSubStrLen <= p.length) {
            result.push(p);
            lastLongestSubStrLen = p.length;
            continue;
        }
    }
    return result;
    

    }

    It will work! But, my concern here you have nested loops. We need to optimise that for performance.

    Please note you need modify the if condition I just altered for error handling. Also, I didn't check the worst cases. For eg: the first occurrence of repeated substring is length of 2, and the next once is length of 3, then you might want to clear all the elements before pushing a new one.

    Happy Coding!