Largest substring of str2 which is a prefix of str1 | Function to return the largest substring in str2 which is a prefix of str1 ; To store the index in str2 which matches the prefix in str1 ; While there are characters left in str1 ; If the prefix is not found in str2 ; Remove the last character ; Prefix found ; No substring found in str2 that matches the prefix of str1 ; Driver code
function findPrefix ( str1 , str2 ) { var pos = - 1 ; while ( str1 . length != 0 ) { if ( ! str2 . includes ( str1 ) ) str1 = str1 . substring ( 0 , str1 . length - 1 ) else { pos = str2 . includes ( str1 ) ; break ; } } if ( pos == - 1 ) return " " ; return str1 ; } var str1 = " " ; var str2 = " " ; document . write ( findPrefix ( str1 , str2 ) ) ;