Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<bool> findAnswer(vector<int>& parent, string s) {
- const int n = parent.size();
- vector<bool> ans(n, false);
- unordered_map<int, vector<int>> mpp;
- for(int i=0; i<n; i++){
- mpp[parent[i]].push_back(i);
- }
- for(auto& el: mpp){
- sort(el.second.begin(), el.second.end());
- }
- auto isPalindrome = [](string& str){
- int i = 0, j = str.size()-1;
- while(i < j) if(str[i++]!=str[j--]) return false;
- return true;
- };
- string DFSstring;
- function<void(int)> dfs = [&](int x){
- for(int child: mpp[x]) dfs(child);
- DFSstring += s[x];
- };
- for(int i=0; i<n; i++){
- DFSstring = "";
- dfs(i);
- ans[i] = isPalindrome(DFSstring);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment