Find all strings that match specific pattern in a dictionary | Python3 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
def check ( pattern , word ) : NEW_LINE INDENT if ( len ( pattern ) != len ( word ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT ch = [ 0 for i in range ( 128 ) ] NEW_LINE Len = len ( word ) NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( ch [ ord ( pattern [ i ] ) ] == 0 ) : NEW_LINE INDENT ch [ ord ( pattern [ i ] ) ] = word [ i ] NEW_LINE DEDENT elif ( ch [ ord ( pattern [ i ] ) ] != word [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findMatchedWords ( Dict , pattern ) : NEW_LINE INDENT Len = len ( pattern ) NEW_LINE for word in range ( len ( Dict ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( check ( pattern , Dict [ word ] ) ) : NEW_LINE INDENT print ( Dict [ word ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT Dict = [ " abb " , " abc " , " xyz " , " xyy " ] NEW_LINE pattern = " foo " NEW_LINE findMatchedWords ( Dict , pattern ) NEW_LINE