🎯Zoho Is Hiring
Role : Software Developer
Qualification:- any
Batch:- any
Location : Chennai
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
Role : Software Developer
Qualification:- any
Batch:- any
Location : Chennai
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
👍2🥰1
📌IT learning courses
📌All programing courses
📌Abdul bari courses
📌Ashok IT
100 rupees
Contact:- @meterials_available
📌All programing courses
📌Abdul bari courses
📌Ashok IT
100 rupees
Contact:- @meterials_available
❤2
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
const int MOD = 1e9 + 7;
vector<vector<int>> tree;
vector<int> a;
unordered_map<int, int> countMap;
bool checkPalindrome(unordered_map<int, int>& countMap) {
int oddCount = 0;
for (auto& it : countMap) {
if (it.second % 2 != 0) oddCount++;
if (oddCount > 1) return false;
}
return true;
}
int dfs(int node) {
int ans = 0;
countMap[a[node]]++;
if (checkPalindrome(countMap)) {
ans = 1;
}
for (auto& child : tree[node]) {
ans += dfs(child);
ans %= MOD;
}
countMap[a[node]]--; // Backtrack to remove the current node's count
return ans;
}
int main() {
int n;
cin >> n;
tree.resize(n + 1);
a.resize(n + 1);
vector<int> par(n + 1);
for (int i = 2; i <= n; i++) {
cin >> par[i];
tree[par[i]].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = dfs(1);
cout << ans << endl;
return 0;
}
c++
Palindromic subtrees
HackWithInfy
@allcoding1
#include <vector>
#include <unordered_map>
using namespace std;
const int MOD = 1e9 + 7;
vector<vector<int>> tree;
vector<int> a;
unordered_map<int, int> countMap;
bool checkPalindrome(unordered_map<int, int>& countMap) {
int oddCount = 0;
for (auto& it : countMap) {
if (it.second % 2 != 0) oddCount++;
if (oddCount > 1) return false;
}
return true;
}
int dfs(int node) {
int ans = 0;
countMap[a[node]]++;
if (checkPalindrome(countMap)) {
ans = 1;
}
for (auto& child : tree[node]) {
ans += dfs(child);
ans %= MOD;
}
countMap[a[node]]--; // Backtrack to remove the current node's count
return ans;
}
int main() {
int n;
cin >> n;
tree.resize(n + 1);
a.resize(n + 1);
vector<int> par(n + 1);
for (int i = 2; i <= n; i++) {
cin >> par[i];
tree[par[i]].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = dfs(1);
cout << ans << endl;
return 0;
}
c++
Palindromic subtrees
HackWithInfy
@allcoding1
👍2
#include <iostream>
#include <string>
using namespace std;
bool isGoodString(string s) {
int count = 0;
for (char c : s) {
if (c == 'a') {
count++;
} else {
count--;
}
if (count < 0) {
return false;
}
}
return count == 0;
}
int main() {
string s = "aaabbbaaa";
cout << "Is the string good? " << (isGoodString(s) ? "Yes" : "No") << endl;
return 0;
}
Count Good Triples
C++
HackWithInfy
@allcoding1
#include <string>
using namespace std;
bool isGoodString(string s) {
int count = 0;
for (char c : s) {
if (c == 'a') {
count++;
} else {
count--;
}
if (count < 0) {
return false;
}
}
return count == 0;
}
int main() {
string s = "aaabbbaaa";
cout << "Is the string good? " << (isGoodString(s) ? "Yes" : "No") << endl;
return 0;
}
Count Good Triples
C++
HackWithInfy
@allcoding1
👍2