Loading...
Loading...
You are given a tree with n nodes labeled 0 to n - 1, described by a list edges where each edges[i] = [a, b] is an undirected edge between nodes a and b. You are also given a string letters of length n, where letters[v] is the letter written on node v, and a string s.
A path is a sequence of distinct nodes in which each pair of consecutive nodes is joined by an edge. A path spells the string formed by reading its nodes' letters in order. A single node is a path too, and it spells one letter.
Two paths are different when their sequences of nodes differ. So a path and its reverse are two different paths, and both are counted when both spell the string.
Return an integer array answer of length s.length, where answer[i] is the number of paths that spell the first i + 1 letters of s.
Input: n = 3
edges = [[0, 1],
[1, 2]]
letters = "aba"
s = "aba"
Output: [2, 2, 2]
Explanation: "a" is spelled by node 0 and by node 2. "ab" is spelled by 0 -> 1 and by 2 -> 1. "aba" is spelled by 0 -> 1 -> 2 and by its reverse 2 -> 1 -> 0. The sequence 0 -> 1 -> 0 does not count, because it uses node 0 twice.
Input: n = 5
edges = [[0, 1],
[0, 2],
[0, 3],
[0, 4]]
letters = "baaab"
s = "abab"
Output: [3, 3, 6, 0]
Explanation: Nodes 1, 2 and 3 hold "a", and node 0 holds "b". Each of the three "a" nodes steps to node 0, so 3 paths spell "ab". From node 0 the path can go on to either of the two other "a" nodes, so 3 * 2 = 6 paths spell "aba". The only neighbor of a leaf is node 0, which is already used, so no path spells "abab".
Input: n = 1
edges = []
letters = "z"
s = "zz"
Output: [1, 0]
Explanation: Node 0 alone spells "z". Spelling "zz" would need two different nodes.
edges.length == n - 1edges[i] = [a, b] with 0≤ a, b ≤n−1 and a != bletters.length == ns.length ≤100letters and s consist of lowercase English letters.