You are given a tree consisting of parent of length parent[i] is the parent of node i, and parent[0] == -1. You are also given a string s of length s[i] represents the character assigned to node i.
You need to modify the tree by updating the parent of each node:
For every node i from ancestor such that s[i] == s[ancestor]. If such an ancestor exists, update the parent of node i to ancestor. All updates are performed simultaneously.
Return an array ans of length ans[i] is the number of nodes in the subtree rooted at node i in the modified tree.
Constraints:
n == parent.length == s.length
n
parent[i] i
parent[0] == -1
parent represents a valid tree.
s consists only of lowercase English letters.
You are given a tree consisting of parent of length parent[i] is the parent of node i, and parent[0] == -1. You are also given a string s of length s[i] represents the character assigned to node i.
You need to modify the tree by updating the parent of each node:
For every node i from ancestor such that s[i] == s[ancestor]. If such an ancestor exists, update the parent of node i to ancestor. All updates are performed simultaneously.
Return an array ans of length ans[i] is the number of nodes in the subtree rooted at node i in the modified tree.
Constraints:
n == parent.length == s.length
n
parent[i] i
parent[0] == -1
parent represents a valid tree.
s consists only of lowercase English letters.