Count nodes in each subtree that share the subtree root's label

Quick Overview

Given a tree rooted at node 0 with a lowercase letter on every node, return for each node how many nodes in its subtree carry the same letter. It tests rooting a tree given as undirected edges, combining results from children, and a traversal that handles deep trees of up to 100,000 nodes.

Count nodes in each subtree that share the subtree root's label

Company: Samsung

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a tree (a connected, undirected graph with no cycles) with `n` nodes numbered `0` to `n - 1` and exactly `n - 1` edges. The tree is rooted at node `0`. Each node has a label, a lowercase letter: node `i` has label `labels[i]`. Each entry of `edges` has the form `[a, b]`, meaning there is an edge between nodes `a` and `b`. Return an array `ans` of length `n` where `ans[i]` is the number of nodes in the subtree of node `i` that have the same label as node `i`. ### Function Signature ```python def count_sub_trees(n: int, edges: list[list[int]], labels: str) -> list[int]: ``` ### Rules - An edge `[a, b]` does not say which endpoint is the parent. Parent and child come from rooting the tree at node `0`. - The subtree of node `i` is node `i` together with all of its descendants, so node `i` counts toward `ans[i]` and every answer is at least 1. ### Constraints - `1 <= n <= 10^5` - `len(edges) == n - 1` - `len(edges[i]) == 2` - `0 <= edges[i][0], edges[i][1] < n` - `edges[i][0] != edges[i][1]` - `len(labels) == n` - `labels` contains only lowercase English letters. ### Examples ```text Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd" Output: [2,1,1,1,1,1,1] ``` Node 0 (a) has children 1 (b) and 2 (a), node 1 has children 4 (d) and 5 (c), and node 2 has children 3 (e) and 6 (d). The subtree of node 0 holds two nodes labeled a (nodes 0 and 2), and every other node is the only one with its label inside its own subtree. ```text Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb" Output: [4,2,1,1] ``` Every label is b, so each answer is the subtree size: node 0 covers all 4 nodes, node 1 covers nodes 1 and 2, and nodes 2 and 3 are leaves. ```text Input: n = 3, edges = [[1,0],[2,1]], labels = "aba" Output: [2,1,1] ``` The edges list the child first, but rooted at node 0 the tree is the path 0, 1, 2. Node 0's subtree contains nodes 0 and 2 labeled a, and node 1's subtree has only one b.

Overview: Given a tree rooted at node 0 with a lowercase letter on every node, return for each node how many nodes in its subtree carry the same letter. It tests rooting a tree given as undirected edges, combining results from children, and a traversal that handles deep trees of up to 100,000 nodes.

Samsung logo
Samsung
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a tree (a connected, undirected graph with no cycles) with n nodes numbered 0 to n - 1 and exactly n - 1 edges. The tree is rooted at node 0. Each node has a label, a lowercase letter: node i has label labels[i].

Each entry of edges has the form [a, b], meaning there is an edge between nodes a and b.

Return an array ans of length n where ans[i] is the number of nodes in the subtree of node i that have the same label as node i.

Function Signature

def count_sub_trees(n: int, edges: list[list[int]], labels: str) -> list[int]:

Rules

  • An edge [a, b] does not say which endpoint is the parent. Parent and child come from rooting the tree at node 0 .
  • The subtree of node i is node i together with all of its descendants, so node i counts toward ans[i] and every answer is at least 1.

Constraints

  • 1 <= n <= 10^5
  • len(edges) == n - 1
  • len(edges[i]) == 2
  • 0 <= edges[i][0], edges[i][1] < n
  • edges[i][0] != edges[i][1]
  • len(labels) == n
  • labels contains only lowercase English letters.

Examples

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd"
Output: [2,1,1,1,1,1,1]

Node 0 (a) has children 1 (b) and 2 (a), node 1 has children 4 (d) and 5 (c), and node 2 has children 3 (e) and 6 (d). The subtree of node 0 holds two nodes labeled a (nodes 0 and 2), and every other node is the only one with its label inside its own subtree.

Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb"
Output: [4,2,1,1]

Every label is b, so each answer is the subtree size: node 0 covers all 4 nodes, node 1 covers nodes 1 and 2, and nodes 2 and 3 are leaves.

Input: n = 3, edges = [[1,0],[2,1]], labels = "aba"
Output: [2,1,1]

The edges list the child first, but rooted at node 0 the tree is the path 0, 1, 2. Node 0's subtree contains nodes 0 and 2 labeled a, and node 1's subtree has only one b.