Find Palindromes
Learn this problemProblem statement
You are given a tree T with N nodes and the tree rooted at node 1. Every node has a character C[i] assigned to it. You are given Q queries of the following format:
Query: u
You must find whether string S that is generated for node u is palindromic or not. String S for a node u is generated as follows:
S = Empty
makeString(u)
{
For all v = Child of u
makeString(v)
S += C[u];
}
Children of u should be traversed in order of increasing node number.
Input format
Return Value
Return an integer array of length Q. For each query, return 1 if its generated string S is palindromic; otherwise, return 0.
Note: Use fast I/O
Function
isPalindrome(n: int, edges: int[][], c: char[], queries: int[]) → int[]Examples
Example 1
n = 5edges = [[1, 2], [1, 3], [2, 4], [2, 5]]c = ["a","b","a","b","c"]queries = [1, 2]return = [0, 1]
For node 1, S = "bcbaa"
For node 2, S = "bcb"
Constraints
1 ≤ N, Q ≤ 200000|C[i]| contains lowercase Latin Letters.More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026