import
java.util.*;
public
class
GFG {
static
boolean
isPalindrome(String x)
{
return
x.equals(
new
StringBuilder(x).reverse().toString());
}
static
int
countPalindromeNodes(
HashMap<Integer, List<Integer> > graph,
List<String> weight)
{
int
count =
0
;
Deque<Integer> queue =
new
LinkedList<>();
queue.add(
1
);
while
(!queue.isEmpty()) {
int
node = queue.poll();
if
(isPalindrome(weight.get(node))) {
count++;
}
for
(
int
neighbor : graph.getOrDefault(
node, Collections.emptyList())) {
queue.add(neighbor);
}
}
return
count;
}
public
static
void
main(String[] args)
{
List<String> weight =
new
ArrayList<>(Arrays.asList(
""
,
"abc"
,
"aba"
,
"bcb"
,
"moh"
,
"aa"
));
HashMap<Integer, List<Integer> > graph
=
new
HashMap<>();
graph.put(
1
,
new
ArrayList<>(Arrays.asList(
2
,
5
)));
graph.put(
2
,
new
ArrayList<>(Arrays.asList(
3
,
4
)));
graph.put(
3
,
new
ArrayList<>());
graph.put(
4
,
new
ArrayList<>());
graph.put(
5
,
new
ArrayList<>());
int
result = countPalindromeNodes(graph, weight);
System.out.println(result);
}
}