Count Paths That Can Form a Palindrome in a Tree - Problem

Imagine you have a tree network where each connection (edge) is labeled with a character. Your mission is to find all pairs of nodes where the path between them contains characters that can be rearranged to form a palindrome!

You're given:

  • A tree with n nodes (numbered 0 to n-1) rooted at node 0
  • An array parent where parent[i] is the parent of node i
  • A string s where s[i] is the character on the edge between node i and its parent

Goal: Count all pairs (u, v) where u < v and the characters on the path from u to v can be rearranged to form a palindrome.

Key Insight: A string can form a palindrome if and only if at most one character appears an odd number of times!

Input & Output

example_1.py — Basic Tree
$ Input: parent = [-1,0,0,1,1,2], s = "abaaab"
Output: 10
💡 Note: Tree has edges with characters a,b,a,a,b. Pairs like (0,1) have path 'a' which can form palindrome. Multiple valid pairs exist where characters can be rearranged to form palindromes.
example_2.py — Simple Chain
$ Input: parent = [-1,0,1], s = "aba"
Output: 3
💡 Note: Tree: 0-a-1-b-2. Valid pairs: (0,1) path='a', (1,2) path='b', (0,2) path='ab'. All can form palindromes since single chars and pairs with different chars work.
example_3.py — No Valid Pairs
$ Input: parent = [-1,0,1], s = "abc"
Output: 2
💡 Note: Tree: 0-a-1-b-2. Pairs (0,1) has path 'a', (1,2) has path 'b' - both single characters can form palindromes. Pair (0,2) has path 'ab' with 2 different odd characters, so cannot form palindrome.

Constraints

  • n == parent.length == s.length
  • 1 ≤ n ≤ 105
  • 0 ≤ parent[i] ≤ n - 1 for all i ≥ 1
  • parent[0] == -1
  • parent represents a valid tree
  • s consists of only lowercase English letters
  • s[0] can be any character since it's ignored

Visualization

Tap to expand
Count Paths That Can Form a Palindrome in a Tree INPUT 0 1 2 3 4 5 b a a a b parent: -1 0 0 1 1 2 s: a b a a a b n = 6 nodes ALGORITHM (DFS) 1 Bitmask for Path Use XOR to track char parity (odd/even count) 2 DFS Traversal Compute path mask from root to each node 3 Count Valid Pairs XOR of two masks = 0 or has 1 bit set 4 Use HashMap Store mask frequencies for O(26n) lookup Bitmask Example Node 0: mask = 0 (root) Node 1: mask = 010 (b) Node 2: mask = 001 (a) Node 3: mask = 011 (ba) Node 4: mask = 011 (ba) FINAL RESULT 10 valid pairs All Valid Pairs: (0,1) (0,2) (0,3) (0,4) (0,5) (1,2) (1,3) (2,5) (3,4) (3,5) Example: (3,4) Path: 3 --a-- 1 --a-- 4 Chars: "aa" Palindrome: "aa" OK mask XOR = 011^011 = 0 OK - Verified Key Insight: A string can form a palindrome if at most ONE character has an odd count. Using bitmasks, XOR of path masks gives character parity. Valid pair if XOR result is 0 (all even) or has exactly 1 bit set (one odd). Time: O(26 * n) with HashMap. Path(u,v) mask = mask(u) XOR mask(v) since common prefix cancels out. TutorialsPoint - Count Paths That Can Form a Palindrome in a Tree | DFS + Bitmask Approach
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
34.3K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen