diff --git a/alien-dictionary/PDKhan.cpp b/alien-dictionary/PDKhan.cpp new file mode 100644 index 000000000..9ca24c575 --- /dev/null +++ b/alien-dictionary/PDKhan.cpp @@ -0,0 +1,67 @@ +class Solution { +public: + string alienOrder(vector &words) { + // Write your code here + unordered_map> graph; + unordered_map indegree; + unordered_set 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 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; + } +}; diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/PDKhan.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/PDKhan.cpp new file mode 100644 index 000000000..194a76fcd --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/PDKhan.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + TreeNode* build(vector& preorder, int& pos, vector& 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& preorder, vector& inorder) { + int pos = 0; + + return build(preorder, pos, inorder, 0, inorder.size()); + } +}; diff --git a/longest-palindromic-substring/PDKhan.cpp b/longest-palindromic-substring/PDKhan.cpp new file mode 100644 index 000000000..8e44e39ae --- /dev/null +++ b/longest-palindromic-substring/PDKhan.cpp @@ -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); + } +}; diff --git a/rotate-image/PDKhan.cpp b/rotate-image/PDKhan.cpp new file mode 100644 index 000000000..6766df6ff --- /dev/null +++ b/rotate-image/PDKhan.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + void rotate(vector>& 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()); + } +}; diff --git a/subtree-of-another-tree/PDKhan.cpp b/subtree-of-another-tree/PDKhan.cpp new file mode 100644 index 000000000..19b3c7444 --- /dev/null +++ b/subtree-of-another-tree/PDKhan.cpp @@ -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); + } +};