Skip to content

[PDKhan] WEEK 05 solutions #1376

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
Apr 27, 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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int maxProfit(vector<int>& 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;
}
};
43 changes: 43 additions & 0 deletions encode-and-decode-strings/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -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<string> &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<string> decode(string &str) {
// write your code here
vector<string> result;
int i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int i = 0;


while(i < str.size()){
int j = i;

while(str[j] != ':')
j++;

int len = stoi(str.substr(i, j - i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
};
20 changes: 20 additions & 0 deletions group-anagrams/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> map;
vector<vector<string>> 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;
}
};
67 changes: 67 additions & 0 deletions implement-trie-prefix-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
21 changes: 21 additions & 0 deletions word-break/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> 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()];
}
};