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
def encodeString ( Str ) : NEW_LINE INDENT map = { } NEW_LINE res = " " NEW_LINE i = 0 NEW_LINE for ch in Str : NEW_LINE INDENT if ch not in map : NEW_LINE INDENT map [ ch ] = i NEW_LINE i += 1 NEW_LINE DEDENT res += str ( map [ ch ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def findMatchedWords ( dict , pattern ) : NEW_LINE INDENT Len = len ( pattern ) NEW_LINE hash = encodeString ( pattern ) NEW_LINE for word in dict : NEW_LINE INDENT if ( len ( word ) == Len and encodeString ( word ) == hash ) : NEW_LINE INDENT print ( word , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT dict = [ " abb " , " abc " , " xyz " , " xyy " ] NEW_LINE pattern = " foo " NEW_LINE findMatchedWords ( dict , pattern ) NEW_LINE