Find all strings that match specific pattern in a dictionary | Javascript program to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; Function to print all the strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary ; len is length of the pattern ; For each word in the dictionary ; Driver code
function check ( pattern , word ) { if ( pattern . length != word . length ) return false ; let ch = new Array ( 128 ) ; for ( let i = 0 ; i < 128 ; i ++ ) { ch [ i ] = 0 ; } let Len = word . length ; for ( let i = 0 ; i < Len ; i ++ ) { if ( ch [ pattern [ i ] . charCodeAt ( 0 ) ] == 0 ) { ch [ pattern [ i ] . charCodeAt ( 0 ) ] = word [ i ] ; } else if ( ch [ pattern [ i ] . charCodeAt ( 0 ) ] != word [ i ] ) { return false ; } } return true ; } function findMatchedWords ( dict , pattern ) { let Len = pattern . length ; let result = " " ; for ( let word of dict . values ( ) ) { if ( check ( pattern , word ) ) { result = word + " " + result ; } } document . write ( result ) ; } let dict = new Set ( ) ; dict . add ( " " ) ; dict . add ( " " ) ; dict . add ( " " ) ; dict . add ( " " ) ; let pattern = " " ; findMatchedWords ( dict , pattern ) ;