Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check if DFS Strings Are Palindromes

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3327" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a tree rooted at node 0, consisting of n nodes numbered from 0 to n - 1. The tree is represented by an array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. You are also given a string s of length n, where s[i] is the character assigned to node i. Consider an empty string dfsStr, and define a recursive function dfs(int x) that takes a node x as a parameter and performs the following steps in order: Iterate over each child y of x in increasing order of their numbers, and call dfs(y). Add the character s[x] to the end of the string dfsStr. Note that dfsStr is shared across all recursive calls of dfs. You need to find a boolean array answer of size n, where for each index i from 0 to n - 1, you do the following: Empty the string dfsStr and call dfs(i). If the resulting string dfsStr is a palindrome, then set answer[i] to true. Otherwise, set answer[i] to false. Return the array answer. Example 1: Input: parent = [-1,0,0,1,1,2], s = "aababa" Output: [true,true,false,true,true,true] Explanation: Calling dfs(0) results in the string dfsStr = "abaaba", which is a palindrome. Calling dfs(1) results in the string dfsStr = "aba", which is a palindrome. Calling dfs(2) results in the string dfsStr = "ab", which is not a palindrome. Calling dfs(3) results in the string dfsStr = "a", which is a palindrome. Calling dfs(4) results in the string dfsStr = "b", which is a palindrome. Calling dfs(5) results in the string dfsStr = "a", which is a palindrome. Example 2: Input: parent = [-1,0,0,0,0], s = "aabcb" Output: [true,true,true,true,true] Explanation: Every call on dfs(x) results in a palindrome string. 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 only of lowercase English letters.

Explanation

Here's the breakdown of the solution:

  • Build the Tree: Create an adjacency list representation of the tree from the parent array. This allows efficient traversal of the tree's children.
  • DFS and Palindrome Check: For each node i, perform a Depth-First Search (DFS) starting from that node. During the DFS, construct the dfsStr as specified in the problem statement. After the DFS completes, check if the dfsStr is a palindrome.
  • Store Results: Store the boolean result (palindrome or not) for each node in the answer array.

  • Runtime Complexity: O(n^2), where n is the number of nodes in the tree. This is because, in the worst case, each DFS traversal could visit all n nodes, and palindrome check takes O(n). Storage Complexity: O(n). This accounts for the adjacency list representation of the tree, the answer array, and the maximum depth of the recursion stack, which can be n in a skewed tree.

Code

    def solve():
    def is_palindrome(s):
        return s == s[::-1]

    def dfs(node, adj, s):
        nonlocal dfs_str
        for child in sorted(adj[node]):
            dfs(child, adj, s)
        dfs_str += s[node]

    n = len(parent)
    adj = [[] for _ in range(n)]
    for i in range(1, n):
        adj[parent[i]].append(i)

    answer = [False] * n
    for i in range(n):
        global dfs_str
        dfs_str = ""
        dfs(i, adj, s)
        answer[i] = is_palindrome(dfs_str)

    return answer

parent = [-1,0,0,1,1,2]
s = "aababa"
print(solve())

parent = [-1,0,0,0,0]
s = "aabcb"
print(solve())

More from this blog

C

Chatmagic blog

2894 posts