From c9aa7ddae9917b49313bdbd205031f6d12136bce Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 26 Apr 2025 12:17:11 -0700 Subject: [PATCH 1/5] Best Time to Buy And Sell Stock solution --- best-time-to-buy-and-sell-stock/PDKhan.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/PDKhan.cpp diff --git a/best-time-to-buy-and-sell-stock/PDKhan.cpp b/best-time-to-buy-and-sell-stock/PDKhan.cpp new file mode 100644 index 000000000..57352ca19 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/PDKhan.cpp @@ -0,0 +1,16 @@ +class Solution { + public: + int maxProfit(vector& prices) { + int min = INT_MAX; + int profit = 0; + + for(int i = 0; i < prices.size(); i++){ + if(prices[i] < min) + min = prices[i]; + + profit = max(profit, prices[i] - min); + } + + return profit; + } + }; From 62af3c673dc67235e24bc745e0b8532bd7e2f870 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 26 Apr 2025 12:17:27 -0700 Subject: [PATCH 2/5] Group Anagrams solution --- group-anagrams/PDKhan.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 group-anagrams/PDKhan.cpp diff --git a/group-anagrams/PDKhan.cpp b/group-anagrams/PDKhan.cpp new file mode 100644 index 000000000..227b0afcc --- /dev/null +++ b/group-anagrams/PDKhan.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + vector> groupAnagrams(vector& strs) { + unordered_map> map; + vector> result; + + for(const string& s : strs){ + string key = s; + sort(key.begin(), key.end()); + + map[key].push_back(s); + } + + for(auto& pair : map){ + result.push_back(pair.second); + } + + return result; + } + }; From 124da3ba1e2e3ed61f7d03179b42958c2368729b Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 26 Apr 2025 12:17:44 -0700 Subject: [PATCH 3/5] Endcode and Decode Strings solution --- encode-and-decode-strings/PDKhan.cpp | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 encode-and-decode-strings/PDKhan.cpp diff --git a/encode-and-decode-strings/PDKhan.cpp b/encode-and-decode-strings/PDKhan.cpp new file mode 100644 index 000000000..6cc070a75 --- /dev/null +++ b/encode-and-decode-strings/PDKhan.cpp @@ -0,0 +1,43 @@ +class Solution { + public: + /* + * @param strs: a list of strings + * @return: encodes a list of strings to a single string. + */ + string encode(vector &strs) { + // write your code here + string code; + + for(const string& s : strs){ + code += to_string(s.size()) + ":" + s; + } + + return code; + } + + /* + * @param str: A string + * @return: decodes a single string to a list of strings + */ + vector decode(string &str) { + // write your code here + vector result; + int i; + + while(i < str.size()){ + int j = i; + + while(str[j] != ':') + j++; + + int len = stoi(str.substr(i, j - i); + string word = str.substr(j + 1, len); + + result.push_back(word); + + i = j + 1 + len; + } + + return result; + } + }; From 7ed301a8fe025477b10ebe37d7a8c30fc3fb5ed0 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 26 Apr 2025 12:18:01 -0700 Subject: [PATCH 4/5] Implement Trie Prefix Tree solution --- implement-trie-prefix-tree/PDKhan.cpp | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 implement-trie-prefix-tree/PDKhan.cpp diff --git a/implement-trie-prefix-tree/PDKhan.cpp b/implement-trie-prefix-tree/PDKhan.cpp new file mode 100644 index 000000000..d7ff98b53 --- /dev/null +++ b/implement-trie-prefix-tree/PDKhan.cpp @@ -0,0 +1,67 @@ +class TrieNode{ + public: + TrieNode* children[26]; + bool isEnd; + + TrieNode() { + isEnd = false; + + for(int i = 0; i < 26; i++) + children[i] = nullptr; + } + }; + + class Trie { + public: + TrieNode* root; + + Trie() { + root = new TrieNode(); + } + + void insert(string word) { + TrieNode* node = root; + + for(char ch : word){ + int index = ch - 'a'; + + if(node->children[index] == nullptr){ + node->children[index] = new TrieNode(); + } + + node = node->children[index]; + } + + node->isEnd = true; + } + + bool search(string word) { + TrieNode* node = root; + + for(char ch : word){ + int index = ch - 'a'; + + if(node->children[index] == nullptr) + return false; + + node = node->children[index]; + } + + return node->isEnd; + } + + bool startsWith(string prefix) { + TrieNode* node = root; + + for(char ch : prefix){ + int index = ch - 'a'; + + if(node->children[index] == nullptr) + return false; + + node = node->children[index]; + } + + return true; + } + }; From 40d5967ee9c2997d1b8f92b3e9db2325fc5f507c Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 26 Apr 2025 12:18:14 -0700 Subject: [PATCH 5/5] Work Break solution --- word-break/PDKhan.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 word-break/PDKhan.cpp diff --git a/word-break/PDKhan.cpp b/word-break/PDKhan.cpp new file mode 100644 index 000000000..9100efbff --- /dev/null +++ b/word-break/PDKhan.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + bool wordBreak(string s, vector& wordDict) { + vector dp(s.length()+1, false); + + dp[0] = true; + + for(int i = 1; i <= s.length(); i++){ + for(string& word : wordDict){ + int len = word.length(); + if(i - len >= 0 && s.substr(i-len, len) == word) + dp[i] = dp[i - len]; + + if(dp[i]) + break; + } + } + + return dp[s.length()]; + } + };