Find all strings that match specific pattern in a dictionary | Function to encode given string ; for each character in given string ; If the character is occurring for the first time , assign next unique number to that char ; append the number associated with current character into the output string ; 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 ; encode the string ; for each word in the dictionary array ; If size of pattern is same as size of current dictionary word and both pattern and the word has same hash , print the word ; Driver code
function encodeString ( str ) { let map = new Map ( ) ; let res = " " ; let i = 0 ; let ch ; for ( let j = 0 ; j < str . length ; j ++ ) { ch = str [ j ] ; if ( ! map . has ( ch ) ) map . set ( ch , i ++ ) ; res += map . get ( ch ) ; } return res ; } function findMatchedWords ( dict , pattern ) { let len = pattern . length ; let hash = encodeString ( pattern ) ; for ( let word = 0 ; word < dict . length ; word ++ ) { if ( dict [ word ] . length == len && encodeString ( dict [ word ] ) == ( hash ) ) document . write ( dict [ word ] + " " ) ; } } let dict = [ " " , " " , " " , " " ] ; let pattern = " " ; findMatchedWords ( dict , pattern ) ;