//User function Template for Java
//User function Template for Java
class Solution
{
static class TrieNode
{
TrieNode child[] = new TrieNode[26];
boolean isEnd = false ;
ArrayList<String> word= new ArrayList<>() ;
public TrieNode()
{
for(int i =0 ;i<26;i++)
{
child[i] = null ;
}
}
}
static void insert(TrieNode root , String key )
{
TrieNode node = root ;
int index ;
for(int i =0 ;i<key.length();i++)
{
char c = key.charAt(i);
if(Character.isLowerCase(c))
{
continue ;
}
index = c - 'A';
if(node.child[index] == null)
{
node.child[index] = new TrieNode();
}
node = node.child[index];
}
node.word.add(key);
node.isEnd =true ;
}
static void printAll(TrieNode root)
{
TrieNode node = root ;
if(node.isEnd)
{
Collections.sort(node.word);
for(int i =0 ;i <node.word.size();i++)
{
System.out.print(node.word.get(i) + " ");
}
}
for(int i=0;i<26;i++)
{
if(node.child[i] != null)
{
printAll(root.child[i]);
}
}
}
static boolean search(TrieNode root , String key)
{
TrieNode node = root ;
for(int i =0 ;i<key.length();i++)
{
int index = key.charAt(i) - 'A';
if(node.child[index] == null)
{
return false ;
}
node = node.child[index];
}
printAll(node);
return true ;
}
static void findAllWords(String[] dict, String pattern)
{
//Your code here
TrieNode root = new TrieNode();
for(String s : dict)
{
insert(root,s);
}
if(!search(root,pattern))
{
System.out.print("No match found");
}
}
}