import
java.util.*;
public
class
Main
{
static
int
CHILDREN =
26
;
static
int
MAX =
100
;
static
int
count =
0
;
static
class
trie {
public
trie[] child;
public
boolean
endOfWord;
public
trie()
{
endOfWord =
false
;
child =
new
trie[CHILDREN];
}
}
static
trie createNode()
{
trie temp =
new
trie();
temp.endOfWord =
false
;
for
(
int
i =
0
; i < CHILDREN ; i++)
{
temp.child[i] =
null
;
}
return
temp;
}
static
void
insertRecursively(trie itr, String str,
int
i)
{
if
(i < str.length())
{
int
index = str.charAt(i) -
'a'
;
if
(itr.child[index] ==
null
)
{
itr.child[index] = createNode();
}
insertRecursively(itr.child[index], str, i +
1
);
}
else
{
itr.endOfWord =
true
;
}
}
static
void
insert(trie itr, String str)
{
insertRecursively(itr, str,
0
);
}
static
boolean
isLeafNode(trie root)
{
return
root.endOfWord !=
false
;
}
static
void
displayContent(trie root,
char
[] str,
int
level)
{
if
(isLeafNode(root))
{
str[level] =
'\0'
;
count++;
if
(count==
2
)
{
System.out.println(
"any"
);
}
else
{
System.out.println(str);
}
}
for
(
int
i =
0
; i < CHILDREN; i++)
{
if
(root.child[i] !=
null
)
{
str[level] = (
char
)(i + (
int
)
'a'
);
displayContent(root.child[i], str, level +
1
);
}
}
}
static
void
display(trie itr)
{
int
level =
0
;
char
[] str =
new
char
[MAX];
displayContent(itr, str, level);
}
public
static
void
main(String[] args) {
trie root = createNode();
insert(root,
"their"
);
insert(root,
"there"
);
insert(root,
"answer"
);
insert(root,
"any"
);
display(root);
}
}