import
java.util.Vector;
public
class
Dictionary
{
private
Dictionary[] children;
private
boolean
endOfWord;
public
Dictionary() {
this
.endOfWord =
false
;
this
.children =
new
Dictionary[
26
];
for
(
int
i =
0
; i <
26
; i++) {
this
.children[i] =
null
;
}
}
public
void
addWord(String word) {
Dictionary pCrawl =
this
;
for
(
int
i =
0
; i < word.length(); i++) {
int
index = word.charAt(i) -
'a'
;
if
(pCrawl.children[index] ==
null
) {
pCrawl.children[index] =
new
Dictionary();
}
pCrawl = pCrawl.children[index];
}
pCrawl.endOfWord =
true
;
}
public
void
search(String word,
boolean
[] found, String curr_found,
int
pos) {
Dictionary pCrawl =
this
;
if
(pos == word.length()) {
if
(pCrawl.endOfWord) {
System.out.println(
"Found: "
+ curr_found);
found[
0
] =
true
;
}
return
;
}
if
(word.charAt(pos) ==
'.'
) {
for
(
int
i =
0
; i <
26
; i++) {
if
(pCrawl.children[i] !=
null
) {
pCrawl.children[i].search(word, found, curr_found + (
char
)(
'a'
+ i), pos +
1
);
}
}
}
else
{
if
(pCrawl.children[word.charAt(pos) -
'a'
] !=
null
) {
pCrawl.children[word.charAt(pos) -
'a'
].search(word, found, curr_found + word.charAt(pos), pos +
1
);
}
}
}
public
void
searchUtil(String word) {
boolean
[] found = {
false
};
System.out.println(
"Searching for \""
+ word +
"\""
);
this
.search(word, found,
""
,
0
);
if
(!found[
0
]) {
System.out.println(
"No Word Found...!!"
);
}
}
public
static
void
searchPattern(Vector<String> words, String str) {
Dictionary obj =
new
Dictionary();
for
(String word : words) {
obj.addWord(word);
}
obj.searchUtil(str);
}
public
static
void
main(String[] args)
{
Vector<String> words =
new
Vector<String>();
words.add(
"data"
);
words.add(
"date"
);
words.add(
"month"
);
String str =
"d.t."
;
searchPattern(words, str);
}
}