Skip to content

[PDKhan] WEEK 15 solutions #1651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions alien-dictionary/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Solution {
public:
string alienOrder(vector<string> &words) {
// Write your code here
unordered_map<char, unordered_set<char>> graph;
unordered_map<char, int> indegree;
unordered_set<char> all_chars;

for (const string& word : words) {
for (char c : word) {
all_chars.insert(c);
if (!indegree.count(c))
indegree[c] = 0;
}
}

for(int i = 0; i < words.size() - 1; i++){
string word1 = words[i];
string word2 = words[i + 1];
int len = min(word1.size(), word2.size());
bool found = false;

for(int j = 0; j < len; j++){
char ch1 = word1[j];
char ch2 = word2[j];

if(ch1 != ch2){
if(graph[ch1].count(ch2) == 0){
graph[ch1].insert(ch2);
indegree[ch2]++;
}

found = true;
break;
}
}

if(!found && word1.size() > word2.size())
return "";
}

queue<char> q;
for(char c : all_chars){
if(indegree[c] == 0)
q.push(c);
}

string order;

while(!q.empty()){
char cur = q.front();
q.pop();

order += cur;

for(char next : graph[cur]){
if(--indegree[next] == 0)
q.push(next);
}
}

if(order.size() != all_chars.size())
return "";

return order;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
TreeNode* build(vector<int>& preorder, int& pos, vector<int>& inorder, int start, int end) {
if(start >= end)
return nullptr;

int i;

for(i = start; i < end; i++){
if(preorder[pos] == inorder[i])
break;
}

TreeNode* root = new TreeNode(preorder[pos]);

pos++;

root->left = build(preorder, pos, inorder, start, i);
root->right = build(preorder, pos, inorder, i + 1, end);

return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int pos = 0;

return build(preorder, pos, inorder, 0, inorder.size());
}
};
26 changes: 26 additions & 0 deletions longest-palindromic-substring/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
void compare(string s, int left, int right, int& max_start, int& max_end){
while(left >= 0 && right < s.length() && s[left] == s[right]){
left--;
right++;
}

if(max_end - max_start < right - left - 1){
max_start = left + 1;
max_end = right - 1;
}
}

string longestPalindrome(string s) {
int max_start = 0;
int max_end = 0;

for(int i = 0; i < s.length(); i++){
compare(s, i, i, max_start, max_end);
compare(s, i, i + 1, max_start, max_end);
}

return s.substr(max_start, max_end - max_start + 1);
}
};
15 changes: 15 additions & 0 deletions rotate-image/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();

for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
swap(matrix[i][j], matrix[j][i]);
}
}

for(int i = 0; i < n; i++)
reverse(matrix[i].begin(), matrix[i].end());
}
};
21 changes: 21 additions & 0 deletions subtree-of-another-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool compare_tree(TreeNode* root, TreeNode* subRoot) {
if(!root && !subRoot)
return true;
else if(!root || !subRoot || root->val != subRoot->val)
return false;

return compare_tree(root->left, subRoot->left) && compare_tree(root->right, subRoot->right);
}

bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if(!root)
return false;

if(compare_tree(root, subRoot))
return true;

return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
};