import
java.util.HashMap;
import
java.util.Map;
import
java.util.Vector;
class
TrieNode
{
Map<Character, TrieNode> children;
boolean
isEndOfWord;
Map<Character, Integer> num;
public
TrieNode() {
this
.children =
new
HashMap<>();
this
.isEndOfWord =
false
;
this
.num =
new
HashMap<>();
}
}
public
class
Main {
static
TrieNode root;
static
TrieNode getNewTrieNode() {
TrieNode pNode =
new
TrieNode();
pNode.isEndOfWord =
false
;
return
pNode;
}
static
void
insertWord(String word) {
TrieNode current = root;
char
s;
for
(
int
i =
0
; i < word.length(); i++) {
s = word.charAt(i);
if
(!current.children.containsKey(s)) {
TrieNode p = getNewTrieNode();
current.children.put(s, p);
current.num.put(s,
1
);
}
else
{
current.num.put(s, current.num.get(s) +
1
);
}
current = current.children.get(s);
}
current.isEndOfWord =
true
;
}
static
int
countWords(Vector<String> words, String prefix) {
root = getNewTrieNode();
int
n = words.size();
for
(
int
i =
0
; i < n; i++) {
insertWord(words.get(i));
}
TrieNode current = root;
char
s;
int
wordCount =
0
;
for
(
int
i =
0
; i < prefix.length(); i++) {
s = prefix.charAt(i);
if
(!current.children.containsKey(s)) {
wordCount =
0
;
break
;
}
wordCount = current.num.get(s);
current = current.children.get(s);
}
return
wordCount;
}
public
static
void
main(String[] args)
{
Vector<String> words =
new
Vector<>();
words.add(
"apk"
);
words.add(
"app"
);
words.add(
"apple"
);
words.add(
"arp"
);
words.add(
"array"
);
String prefix =
"ap"
;
System.out.println(countWords(words, prefix));
}
}