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.

Visualization

Tap to expand
Tree Palindrome Path DetectionROOTmask: 0000...01mask: 00012mask: 001034abacBitmask HashMapmask 0000: count 1mask 0001: count 1mask 0010: count 1Palindrome Check:Same mask ORDiffer by 1 bitCompatible Path Detection๐Ÿ“ŠXOR LogicโšกO(nร—26) Time๐ŸŽฏSingle Pass
Understanding the Visualization
1
Build Network Map
Convert parent array into adjacency list representing the tree structure
2
Start Journey
Begin DFS from root with bitmask=0, tracking character frequency parity
3
Check Compatibility
At each node, count paths that can combine with current path to form palindrome
4
Update Registry
Add current path's bitmask to hashmap for future compatibility checks
5
Continue Exploration
Recurse to children with updated bitmasks representing new character patterns
Key Takeaway
๐ŸŽฏ Key Insight: Using bitmasks to represent character frequency parity allows us to detect palindrome compatibility in O(1) time using XOR operations, making the overall solution optimal at O(nร—26).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยท26)

O(n) for DFS traversal ร— O(26) for checking each possible single-bit difference

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

HashMap storing at most n different bitmask states during traversal

n
2n
โšก Linearithmic Space

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
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