You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given a tree that contains N nodes, each containing an integer u which corresponds to a lowercase character c in the string s using 1-based indexing.
5
+
You are required to answer Q queries of type [u, c], where u is an integer and c is a lowercase letter. The query result is the number of nodes in the subtree of node u containing c.
A pointer to the root node, an array list containing Q queries of type [u, c], and a string s
10
+
Constraints
11
+
N and Q are the integers between 1 and 1,000,000
12
+
u is an integer between 1 and N
13
+
s is of the length of N, containing only lowercase letters
14
+
c is a lowercase letter contained in string s
15
+
Node 1 is the root of the tree
16
+
Output
17
+
An integer array containing the response to each query
18
+
Example
19
+
1(a)
20
+
/ \
21
+
2(b) 3(a)
22
+
s = "aba"
23
+
RootNode = 1
24
+
query = [[1, 'a']]
25
+
Note: Node 1 corresponds to first letter 'a', Node 2 corresponds to second letter of the string 'b', Node 3 corresponds to third letter of the string 'a'.
26
+
output = [2]
27
+
Both Node 1 and Node 3 contain 'a', so the number of nodes within the subtree of Node 1 containing 'a' is 2.
0 commit comments