Longest string which is prefix string of at least two strings | Javascript program to find longest string which is prefix string of at least two strings ; Function to find Max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current string or not ; If atleast 2 string have that character ; Recursive call to i + 1 ; Initialising strings ; push strings into vectors .
let max1 = 0 ; function MaxLength ( v , i , m ) { if ( i >= m ) { return m - 1 ; } for ( let k = 0 ; k < 26 ; k ++ ) { let c = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + k ) ; let v1 = [ ] ; for ( let j = 0 ; j < v . length ; j ++ ) { if ( v [ j ] [ i ] == c ) { v1 . push ( v [ j ] ) ; } } if ( v1 . length >= 2 ) { max1 = Math . max ( max1 , MaxLength ( v1 , i + 1 , m ) ) ; } else { max1 = Math . max ( max1 , i - 1 ) ; } } return max1 ; } let s1 , s2 , s3 , s4 , s5 ; s1 = " " ; s2 = " " ; s3 = " " ; s4 = " " ; s5 = " " ; let v = [ ] ; v . push ( s1 ) ; v . push ( s2 ) ; v . push ( s3 ) ; v . push ( s4 ) ; v . push ( s5 ) ; let m = v [ 0 ] . length ; document . write ( MaxLength ( v , 0 , m ) + 1 + " " ) ;