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 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
import java . util . * ; class GFG { static boolean check ( String pattern , String word ) { if ( pattern . length ( ) != word . length ( ) ) return false ; int [ ] ch = new int [ 128 ] ; int Len = word . length ( ) ; for ( int i = 0 ; i < Len ; i ++ ) { if ( ch [ ( int ) pattern . charAt ( i ) ] == 0 ) { ch [ ( int ) pattern . charAt ( i ) ] = word . charAt ( i ) ; } else if ( ch [ ( int ) pattern . charAt ( i ) ] != word . charAt ( i ) ) { return false ; } } return true ; } static void findMatchedWords ( HashSet < String > dict , String pattern ) { int Len = pattern . length ( ) ; String result = " ▁ " ; for ( String word : dict ) { if ( check ( pattern , word ) ) { result = word + " ▁ " + result ; } } System . out . print ( result ) ; } public static void main ( String [ ] args ) { HashSet < String > dict = new HashSet < String > ( ) ; dict . add ( " abb " ) ; dict . add ( " abc " ) ; dict . add ( " xyz " ) ; dict . add ( " xyy " ) ; String pattern = " foo " ; findMatchedWords ( dict , pattern ) ; } }