Longest repeating and non | Returns the longest repeating non - overlapping substring in str ; building table in bottom - up manner ; ( j - i ) > LCSRe [ i - 1 ] [ j - 1 ] to remove overlapping ; updating maximum length of the substring and updating the finishing index of the suffix ; If we have non - empty result , then insert all characters from first character to last character of String ; Driver program to test the above function
function longestRepeatedSubstring ( str ) { let n = str . length ; let LCSRe = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { LCSRe [ i ] = new Array ( n + 1 ) ; } for ( let i = 0 ; i < n + 1 ; i ++ ) { for ( let j = 0 ; j < n + 1 ; j ++ ) { LCSRe [ i ] [ j ] = 0 ; } } let i , index = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( let j = i + 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && LCSRe [ i - 1 ] [ j - 1 ] < ( j - i ) ) { LCSRe [ i ] [ j ] = LCSRe [ i - 1 ] [ j - 1 ] + 1 ; if ( LCSRe [ i ] [ j ] > res_length ) { res_length = LCSRe [ i ] [ j ] ; index = Math . max ( i , index ) ; } } else { LCSRe [ i ] [ j ] = 0 ; } } } if ( res_length > 0 ) { for ( i = index - res_length + 1 ; i <= index ; i ++ ) { res += str . charAt ( i - 1 ) ; } } return res ; } let str = " " ; document . write ( longestRepeatedSubstring ( str ) ) ;