Skip to content

Commit 6679bf9

Browse files
authored
Merge pull request #1651 from PDKhan/main
[PDKhan] WEEK 15 solutions
2 parents e2635fa + c675ba0 commit 6679bf9

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

alien-dictionary/PDKhan.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Solution {
2+
public:
3+
string alienOrder(vector<string> &words) {
4+
// Write your code here
5+
unordered_map<char, unordered_set<char>> graph;
6+
unordered_map<char, int> indegree;
7+
unordered_set<char> all_chars;
8+
9+
for (const string& word : words) {
10+
for (char c : word) {
11+
all_chars.insert(c);
12+
if (!indegree.count(c))
13+
indegree[c] = 0;
14+
}
15+
}
16+
17+
for(int i = 0; i < words.size() - 1; i++){
18+
string word1 = words[i];
19+
string word2 = words[i + 1];
20+
int len = min(word1.size(), word2.size());
21+
bool found = false;
22+
23+
for(int j = 0; j < len; j++){
24+
char ch1 = word1[j];
25+
char ch2 = word2[j];
26+
27+
if(ch1 != ch2){
28+
if(graph[ch1].count(ch2) == 0){
29+
graph[ch1].insert(ch2);
30+
indegree[ch2]++;
31+
}
32+
33+
found = true;
34+
break;
35+
}
36+
}
37+
38+
if(!found && word1.size() > word2.size())
39+
return "";
40+
}
41+
42+
queue<char> q;
43+
for(char c : all_chars){
44+
if(indegree[c] == 0)
45+
q.push(c);
46+
}
47+
48+
string order;
49+
50+
while(!q.empty()){
51+
char cur = q.front();
52+
q.pop();
53+
54+
order += cur;
55+
56+
for(char next : graph[cur]){
57+
if(--indegree[next] == 0)
58+
q.push(next);
59+
}
60+
}
61+
62+
if(order.size() != all_chars.size())
63+
return "";
64+
65+
return order;
66+
}
67+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
TreeNode* build(vector<int>& preorder, int& pos, vector<int>& inorder, int start, int end) {
4+
if(start >= end)
5+
return nullptr;
6+
7+
int i;
8+
9+
for(i = start; i < end; i++){
10+
if(preorder[pos] == inorder[i])
11+
break;
12+
}
13+
14+
TreeNode* root = new TreeNode(preorder[pos]);
15+
16+
pos++;
17+
18+
root->left = build(preorder, pos, inorder, start, i);
19+
root->right = build(preorder, pos, inorder, i + 1, end);
20+
21+
return root;
22+
}
23+
24+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
25+
int pos = 0;
26+
27+
return build(preorder, pos, inorder, 0, inorder.size());
28+
}
29+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
void compare(string s, int left, int right, int& max_start, int& max_end){
4+
while(left >= 0 && right < s.length() && s[left] == s[right]){
5+
left--;
6+
right++;
7+
}
8+
9+
if(max_end - max_start < right - left - 1){
10+
max_start = left + 1;
11+
max_end = right - 1;
12+
}
13+
}
14+
15+
string longestPalindrome(string s) {
16+
int max_start = 0;
17+
int max_end = 0;
18+
19+
for(int i = 0; i < s.length(); i++){
20+
compare(s, i, i, max_start, max_end);
21+
compare(s, i, i + 1, max_start, max_end);
22+
}
23+
24+
return s.substr(max_start, max_end - max_start + 1);
25+
}
26+
};

rotate-image/PDKhan.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
void rotate(vector<vector<int>>& matrix) {
4+
int n = matrix.size();
5+
6+
for(int i = 0; i < n; i++){
7+
for(int j = i + 1; j < n; j++){
8+
swap(matrix[i][j], matrix[j][i]);
9+
}
10+
}
11+
12+
for(int i = 0; i < n; i++)
13+
reverse(matrix[i].begin(), matrix[i].end());
14+
}
15+
};

subtree-of-another-tree/PDKhan.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool compare_tree(TreeNode* root, TreeNode* subRoot) {
4+
if(!root && !subRoot)
5+
return true;
6+
else if(!root || !subRoot || root->val != subRoot->val)
7+
return false;
8+
9+
return compare_tree(root->left, subRoot->left) && compare_tree(root->right, subRoot->right);
10+
}
11+
12+
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
13+
if(!root)
14+
return false;
15+
16+
if(compare_tree(root, subRoot))
17+
return true;
18+
19+
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
20+
}
21+
};

0 commit comments

Comments
 (0)