How to find the largest substring between two equal characters in a string with Node.js

1 Answer

0 votes
function GetLongestSubstring(str) {
    const size = str.length;
  
    let start = 0;
    let end = 0;
    let max = 0;
    
    for (let i = 0; i < size - 1; i++) {
        for (let j = i + 1; j < size; j++) {
            if (str[i] == str[j]) {
                let temp = Math.abs(j - i - 1);
                if (temp > max) {
                    max = temp; 
                    start = i + 1;
                    end = j;
                } 
            }
        }
    }
        
    return str.substring(start, end);
}

const str = "zXoDnodejsDekq";

console.log(GetLongestSubstring(str));




/*
run:

nodejs

*/

 



answered Dec 22, 2022 by avibootz
edited Dec 23, 2022 by avibootz
...