text: Find all strings that match specific pattern in a dictionary | Java 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 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
code: import java . io . * ; import java . util . * ; class GFG { static String encodeString ( String str ) { HashMap < Character , Integer > map = new HashMap < > ( ) ; String res = " " ; int i = 0 ; char ch ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) { ch = str . charAt ( j ) ; if ( ! map . containsKey ( ch ) ) map . put ( ch , i ++ ) ; res += map . get ( ch ) ; } return res ; } static void findMatchedWords ( String [ ] dict , String pattern ) { int len = pattern . length ( ) ; String hash = encodeString ( pattern ) ; for ( String word : dict ) { if ( word . length ( ) == len && encodeString ( word ) . equals ( hash ) ) System . out . print ( word + " ▁ " ) ; } } public static void main ( String args [ ] ) { String [ ] dict = { " abb " , " abc " , " xyz " , " xyy " } ; String pattern = " foo " ; findMatchedWords ( dict , pattern ) ; } }