2791. Count Paths That Can Form a Palindrome in a Tree


Problem Description

In this problem, we are given a tree with n nodes, where each node is numbered from 0 to n - 1. The tree is rooted at node 0, and we are provided a parent array named parent that defines the tree's structure. The parent[i] value represents the parent node of node i, and for the root node 0, we have parent[0] == -1 because it does not have any parent.

Along with the tree, we have a string s of length n that assigns characters to the edges in the tree. The character s[i] corresponds to the edge between node i and its parent parent[i]. Note that s[0] can be ignored because it is associated with the root, which has no parent.

The task is to find the number of distinct pairs of nodes (u, v) where u < v and the series of characters assigned to the edges in the path from u to v can be rearranged to form a palindrome. A palindrome is a sequence that reads the same backward and forward.

Intuition

To solve this problem, we need to find all paths in the tree that can create palindromes when the edge characters are rearranged. However, since directly checking every path would be too slow, we approach the problem using a bitwise XOR technique.

We begin with the observation that, for a string of characters to be rearranged into a palindrome, each character must appear an even number of times except for at most one character which can be in the middle. In terms of edges in the path, it means that every character should connect an even number of edges except for at most one.

Translating this idea into an algorithm, we use a bitwise representation to keep track of even and odd counts of edge characters along any path. We create a 26-bit integer to represent the count of each letter in the alphabet, where each bit corresponds to a specific character. If a bit is set, it means an odd count of that character; if it is not set, it means an even count.

Now, through depth-first search (DFS), we traverse the tree starting at the root node and calculate the XOR value representing the character count at each node. For each node, we update the global answer (ans) by counting how many times we've seen this XOR pattern (since the path from the root to this node has the same palindrome check as some previous path), and how many times we've seen this XOR pattern with exactly one bit flipped (since that represents just a single character being odd, which is allowable for palindromes).

The overall process includes accumulating the XOR value while descending through the tree, checking palindromic conditions at each step, and updating the global count of valid paths.

The provided solution keeps track of the counts using a Counter dictionary (cnt), which maps each 26-bit integer to the number of times that bit pattern has previously occurred during the DFS. The dfs() function is called recursively to traverse the tree, and the ans variable, defined as nonlocal, is used to accumulate the total number of palindromic paths.

Learn more about Tree, Depth-First Search, Dynamic Programming and Bitmask patterns.

Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece

Breadth first search can be used to find the shortest path between two nodes in a directed graph.

Solution Approach

The solution uses a depth-first search (DFS) algorithm to traverse the tree, taking advantage of a bitwise representation to encode the frequency of edge characters along the path from the root to any given node.

Data Structures Used:

  • A defaultdict (imported from collections module) named g to represent the adjacency list of the tree, where each node has a list of tuples containing its child node and the corresponding edge character's bit representation.
  • A Counter (also from collections module) named cnt to keep track of the number of times we've seen a particular 26-bit representation of character frequencies.

The DFS Algorithm and Bitwise Patterns:

Within the provided code, the dfs() function is responsible for exploring the tree:

  • It accepts two arguments: i, the current node index, and xor, the sum of edge character bit representations up to this node.
  • ans is a nonlocal variable that accumulates the number of valid paths that can form a palindrome. It is incremented both when an existing xor value is found in cnt and when a single-bit difference version of xor is found in cnt.
  • As the function traverses the tree, it calculates the new xor value for each child by using the bitwise XOR operation (^) between the current xor and the bit representation of the child edge's character.
  • A loop through 26 different bit flips (for k in range(26):) is used to check if there's a valid path that has exactly one character with an odd count, which is still acceptable for palindrome formation. If such a condition is found, ans is also incremented for these occurrences.
  • The cnt dictionary keeps track of how many times we've seen this particular xor pattern by incrementing cnt[x].
  • The recursion continues by calling dfs(j, x) for each child j.

Building the Bit Representation:

  • For each character in the edge, a 26-bit integer is built by shifting 1 to the left ord(s[i]) - ord('a') times, which indicates the position of the character in the alphabet (e.g., 'a' corresponds to shifting 0 times as it's the first letter, 'b' corresponds to shifting 1 time, and so on).
  • This way, each bit of the integer represents the count's parity of a corresponding character on the path from the root to the current node.

Initialization and Invocation:

  • The function countPalindromePaths initializes the adjacency list g and the Counter cnt with {0: 1}, which corresponds to the bit representation of an empty path (all bits set to zero, meaning even count for every character).
  • It then calls dfs(0, 0), starting the traversal from the root node with an initial xor of 0.

By using these techniques, the code efficiently finds all palindromic paths within the tree.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

What is the worst case running time for finding an element in a binary search tree(not necessarily balanced) of size n?

Example Walkthrough

Let's use a small example to illustrate the solution approach. Consider the following tree with n = 4 nodes and its associated parent array and string:

1parent = [-1, 0, 1, 1]
2s = "abca"

This defines a tree where:

  • Node 0 is the root as parent[0] == -1.
  • Node 1 is the child of node 0.
  • Nodes 2 and 3 are children of node 1.

The string s colors the edges as follows:

  • Edge from node 1 to node 0 (the parent) with character 'a'.
  • Edge from node 2 to node 1 with character 'b'.
  • Edge from node 3 to node 1 with character 'c'.

Thus, the tree looks like this:

1    0
2   / 
3  a  
4 /
51
6 / \
7b   c
8/     \
92       3

In the bitwise representation:

  • For edge 'a' (from 0 to 1), the bit is 1 << (ord('a') - ord('a')) = 1 << 0 = 1.
  • For edge 'b' (from 1 to 2), the bit is 1 << (ord('b') - ord('a')) = 1 << 1 = 2.
  • For edge 'c' (from 1 to 3), the bit is 1 << (ord('c') - ord('a')) = 1 << 2 = 4.

Now, we'll walk through the DFS algorithm with this example:

  1. We start at the root node 0 with an initial xor value of 0.

  2. We visit the child node 1. Edge 0-1 is labeled 'a', so the xor value becomes 0 ^ 1 = 1.

  3. At node 1, there are two children. We explore each of them in turn:

    • Visiting node 2, the edge 1-2 is 'b', so the xor becomes 1 ^ 2 = 3. We now have a path 0 - 1 - 2 with edge characters 'a', 'b', which gives a bitwise pattern of 011 telling us the count of character 'a' is odd, 'b' is odd, and 'c' is even (hence not set).
    • Visiting node 3, the edge 1-3 is 'c', so the xor becomes 1 ^ 4 = 5. The path 0 - 1 - 3 has edge characters 'a', 'c', resulting in a bitwise pattern of 101.
  4. For each of these nodes 2 and 3, we check the presence of the xor in cnt and check for each possible single-bit flip.

    • When visiting node 2, the xor value before visiting children is 3, which has not been seen before, so cnt[3] is set to 1.
    • For node 3, similarly, the xor value is 5, and cnt[5] is set to 1.
    • There are no single-bit difference patterns for 3 or 5 in cnt yet, so ans stays the same for now.
  5. We do not increment the global ans during this initial pass as the DFS is still in progress.

  6. Finally, after traversing the entire tree, we find that we have the following pair that can form a palindrome:

    • Nodes (2, 3): The path characters are 'b' and 'c'. By reordering the characters, we can't form a palindrome as there must be an even count of each character or an even count of all characters except one.

At the end of our DFS, we find that there are no valid palindromic paths in this particular example, so the answer (ans) is 0.

Solution Implementation

1from collections import defaultdict, Counter
2
3class Solution:
4    def countPalindromePaths(self, parents: List[int], characters: str) -> int:
5        # Helper function to perform Depth-First Search (DFS)
6        def dfs(node_index: int, xor_value: int):
7            # Accessing the variable 'answer' from the outer scope
8            nonlocal answer
9            for child_index, char_value in adjacency_list[node_index]:
10                # Computing the XOR of the current path's characters
11                new_xor = xor_value ^ char_value
12                # If the XOR matches any of our previous XOR values, it's a palindrome path
13                answer += xor_count[new_xor]
14                # Check if toggling any single bit (corresponding to changing one character)
15                # results in a palindrome path
16                for k in range(26):
17                    answer += xor_count[new_xor ^ (1 << k)]
18                # Count the occurrences of this XOR value along the path
19                xor_count[new_xor] += 1
20                # Recursive DFS call to continue the search
21                dfs(child_index, new_xor)
22            # When backtracking, remove the count of the current node's XOR from the count map
23            xor_count[xor_value] -= 1
24
25        # Size of the tree (number of nodes)
26        n = len(parents)
27        # Adjacency list to represent the tree and store (child_index, binary representation of the character)
28        adjacency_list = defaultdict(list)
29        for i in range(1, n):
30            parent_index = parents[i]
31            adjacency_list[parent_index].append((i, 1 << (ord(characters[i]) - ord('a'))))
32      
33        # The answer to keep track of the number of palindrome paths
34        answer = 0
35        # Counter to store the number of times an XOR value has been seen on a path
36        xor_count = Counter({0: 1})
37      
38        # Start DFS from the root node with XOR value 0 (since no characters have been visited yet)
39        dfs(0, 0)
40      
41        # Return the final count of palindrome paths
42        return answer
43
44# Note: List[int] and the function type signature should ideally be imported:
45# from typing import List
46# if you are testing this code outside of a preset environment like LeetCode,
47# otherwise it would throw a NameError for the undefined name 'List'.
48
1class Solution {
2    // Graph representation: an array of lists, where each list contains pairs (child index, character bitmask)
3    private List<int[]>[] graph;
4    // Counter for each state of the XOR of the path (nodeMask)
5    private Map<Integer, Integer> countMap = new HashMap<>();
6    // Answer to accumulate the number of possible palindrome paths
7    private long answer;
8
9    public long countPalindromePaths(List<Integer> parents, String s) {
10        int n = parents.size();
11        graph = new List[n];
12        countMap.put(0, 1);
13        // Initialize each element of the graph with an empty ArrayList
14        Arrays.setAll(graph, k -> new ArrayList<>());
15        // Create the graph from the parent list
16        for (int i = 1; i < n; ++i) {
17            int parentIndex = parents.get(i);
18            graph[parentIndex].add(new int[] {i, 1 << (s.charAt(i) - 'a')});
19        }
20        // Start DFS traversal from root node=0 with initial XOR value 0
21        dfs(0, 0);
22        return answer;
23    }
24
25    // DFS function to traverse the tree and calculate palindrome paths
26    private void dfs(int node, int nodeMask) {
27        // Iterate for every edge connected to the current node
28        for (int[] edge : graph[node]) {
29            int child = edge[0], charBitmask = edge[1];
30            int xor = nodeMask ^ charBitmask;
31          
32            // Update the answer by counting valid palindrome paths ending at the current node
33            answer += countMap.getOrDefault(xor, 0);
34            // Try flipping each bit to spot palindrome pairs
35            for (int k = 0; k < 26; ++k) {
36                answer += countMap.getOrDefault(xor ^ (1 << k), 0);
37            }
38            // Add the new XOR to the map with a count, if it is already present add to its count
39            countMap.merge(xor, 1, Integer::sum);
40            // Continue to DFS for the child node
41            dfs(child, xor);
42        }
43    }
44}
45
1class Solution {
2public:
3    long long countPalindromePaths(vector<int>& parent, string s) {
4        int nodeCount = parent.size(); // Get the number of nodes in the tree
5        vector<vector<pair<int, int>>> graph(nodeCount); // Create an adjacency list for the graph
6        unordered_map<int, int> countMap; // Store the count of each bitmask
7      
8        countMap[0] = 1; // Initialize the count for an empty path to 1
9
10        // Build the graph from the parent relationships
11        for (int i = 1; i < nodeCount; ++i) {
12            int parentNode = parent[i];
13            // Each edge has a bitmask corresponding to the character of the child node
14            graph[parentNode].emplace_back(i, 1 << (s[i] - 'a'));
15        }
16
17        long long answer = 0; // Store the final count of palindromic paths
18        // Define a lambda function for depth-first search on the graph
19        function<void(int, int)> dfs = [&](int node, int bitmask) {
20            // Traverse all child nodes of the current node
21            for (auto [childNode, value] : graph[node]) {
22                int newXor = bitmask ^ value; // Compute new bitmask after including current node
23                answer += countMap[newXor]; // Increase count if the bitmask represents a palindrome so far
24              
25                // Increase count for each palindromic path ending with one character difference
26                for (int k = 0; k < 26; ++k) {
27                    answer += countMap[newXor ^ (1 << k)];
28                }
29
30                ++countMap[newXor]; // Mark the current path
31                dfs(childNode, newXor); // Recurse for child
32            }
33        };
34
35        dfs(0, 0); // Start DFS from the root node with an empty bitmask
36        return answer; // Return the final count of palindromic paths
37    }
38};
39
1function countPalindromePaths(parent: number[], charSequence: string): number {
2    const nodeCount = parent.length; // Total number of nodes
3    // Graph representation: An array of tuples for each node's children
4    const graph: [number, number][][] = Array.from({ length: nodeCount }, () => []);
5
6    // Construct the graph's adjacency list
7    for (let i = 1; i < nodeCount; ++i) {
8        const charBitMask = 1 << (charSequence.charCodeAt(i) - 97); // Bit-representation for character
9        graph[parent[i]].push([i, charBitMask]);
10    }
11
12    // Map to count occurrences of XOR values
13    const xorCount: Map<number, number> = new Map();
14    xorCount.set(0, 1); // Initial XOR count for root
15
16    let palindromePathsCount = 0; // Variable to track total palindrome paths found
17
18    // Recursive DFS to explore all paths
19    const dfs = (nodeIndex: number, xorValue: number): void => {
20        for (const [childIndex, charMask] of graph[nodeIndex]) {
21            const newXor = xorValue ^ charMask; // Calculate new XOR
22            palindromePathsCount += xorCount.get(newXor) || 0; // Increment paths count if a palindrome is found
23
24            // Check for possible single character difference palindromes
25            for (let k = 0; k < 26; ++k) {
26                palindromePathsCount += xorCount.get(newXor ^ (1 << k)) || 0;
27            }
28
29            // Update the count for the current XOR
30            xorCount.set(newXor, (xorCount.get(newXor) || 0) + 1);
31
32            // Continue DFS with the child node and new XOR
33            dfs(childIndex, newXor);
34        }
35    };
36
37    // Start DFS at the root with initial XOR of 0
38    dfs(0, 0);
39
40    // Return the total number of palindrome paths found
41    return palindromePathsCount;
42}
43
Not Sure What to Study? Take the 2-min Quiz

Which one best describes the time complexity of the following code?

1int factorial(int n) {
2  if (n < 0) {
3    return -1;
4  } else if (n == 0) {
5    return 1;
6  } else {
7    return n * factorial(n - 1);
8  }
9}

Time and Space Complexity

The given Python code defines a method countPalindromePaths that returns the number of palindromic paths in a tree-like structure, where each node is labeled with a character from string s. The tree structure is defined by the parent list, where parent[i] is the parent of the i-th node.

Time Complexity

The time complexity of the code is O(N * 26), where N is the number of nodes in the tree. Here's the breakdown:

  1. The dfs function is called recursively for each node exactly once, resulting in N calls.
  2. Inside the dfs function, for each node i, it iterates over all its children followed by a loop running 26 times (for each lowercase alphabet letter).
  3. Every step inside the inner for loop has constant time operations except for the recursive dfs call that happens once for each child node.

Therefore, we have a single dfs call per node and a fixed number of additional iterations per node, resulting in a time complexity of O(N * 26).

Space Complexity

The space complexity of the code is O(N), where N is the number of nodes in the tree. Here's why:

  1. The g dictionary and cnt counter have at most N entries, as they store vertices and the number of times certain XOR values occur.
  2. The dfs call stack can grow up to O(N) in the case of a tree that degenerates into a linked list.
  3. The other variables like x and ans use constant space.

As a result, the total space complexity is determined by the size of the tree and bookkeeping variables, leading to O(N).

In conclusion, the given Python code has a time complexity of O(N * 26) and a space complexity of O(N).

Learn more about how to find time and space complexity quickly using problem constraints.

Fast Track Your Learning with Our Quick Skills Quiz:

What is the space complexity of the following code?

1int sum(int n) {
2  if (n <= 0) {
3    return 0;
4  }
5  return n + sum(n - 1);
6}

Recommended Readings


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

←
↑TA đŸ‘šâ€đŸ«