Check if two strings have a common substring | javascript program to heck if two strings have common substring ; function to return true if strings have common substring and no if strings have no common substring ; vector for storing character occurrences ; increment vector index for every character of str1 ; checking common substring of str2 in str1 ; driver program
var MAX_CHAR = 26 ; function twoStrings ( s1 , s2 ) { var v = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; i < s1 . length ; i ++ ) v [ s1 [ i ] - ' ' ] = true ; for ( var i = 0 ; i < s2 . length ; i ++ ) if ( v [ s2 [ i ] - ' ' ] ) return true ; return false ; } var str1 = " " ; var str2 = " " ; if ( twoStrings ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;