Skip to content

Commit aa6294a

Browse files
authored
Merge pull request #1376 from PDKhan/main
2 parents e562f5a + 40d5967 commit aa6294a

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int min = INT_MAX;
5+
int profit = 0;
6+
7+
for(int i = 0; i < prices.size(); i++){
8+
if(prices[i] < min)
9+
min = prices[i];
10+
11+
profit = max(profit, prices[i] - min);
12+
}
13+
14+
return profit;
15+
}
16+
};

encode-and-decode-strings/PDKhan.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
/*
4+
* @param strs: a list of strings
5+
* @return: encodes a list of strings to a single string.
6+
*/
7+
string encode(vector<string> &strs) {
8+
// write your code here
9+
string code;
10+
11+
for(const string& s : strs){
12+
code += to_string(s.size()) + ":" + s;
13+
}
14+
15+
return code;
16+
}
17+
18+
/*
19+
* @param str: A string
20+
* @return: decodes a single string to a list of strings
21+
*/
22+
vector<string> decode(string &str) {
23+
// write your code here
24+
vector<string> result;
25+
int i;
26+
27+
while(i < str.size()){
28+
int j = i;
29+
30+
while(str[j] != ':')
31+
j++;
32+
33+
int len = stoi(str.substr(i, j - i);
34+
string word = str.substr(j + 1, len);
35+
36+
result.push_back(word);
37+
38+
i = j + 1 + len;
39+
}
40+
41+
return result;
42+
}
43+
};

group-anagrams/PDKhan.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<vector<string>> groupAnagrams(vector<string>& strs) {
4+
unordered_map<string, vector<string>> map;
5+
vector<vector<string>> result;
6+
7+
for(const string& s : strs){
8+
string key = s;
9+
sort(key.begin(), key.end());
10+
11+
map[key].push_back(s);
12+
}
13+
14+
for(auto& pair : map){
15+
result.push_back(pair.second);
16+
}
17+
18+
return result;
19+
}
20+
};

implement-trie-prefix-tree/PDKhan.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class TrieNode{
2+
public:
3+
TrieNode* children[26];
4+
bool isEnd;
5+
6+
TrieNode() {
7+
isEnd = false;
8+
9+
for(int i = 0; i < 26; i++)
10+
children[i] = nullptr;
11+
}
12+
};
13+
14+
class Trie {
15+
public:
16+
TrieNode* root;
17+
18+
Trie() {
19+
root = new TrieNode();
20+
}
21+
22+
void insert(string word) {
23+
TrieNode* node = root;
24+
25+
for(char ch : word){
26+
int index = ch - 'a';
27+
28+
if(node->children[index] == nullptr){
29+
node->children[index] = new TrieNode();
30+
}
31+
32+
node = node->children[index];
33+
}
34+
35+
node->isEnd = true;
36+
}
37+
38+
bool search(string word) {
39+
TrieNode* node = root;
40+
41+
for(char ch : word){
42+
int index = ch - 'a';
43+
44+
if(node->children[index] == nullptr)
45+
return false;
46+
47+
node = node->children[index];
48+
}
49+
50+
return node->isEnd;
51+
}
52+
53+
bool startsWith(string prefix) {
54+
TrieNode* node = root;
55+
56+
for(char ch : prefix){
57+
int index = ch - 'a';
58+
59+
if(node->children[index] == nullptr)
60+
return false;
61+
62+
node = node->children[index];
63+
}
64+
65+
return true;
66+
}
67+
};

word-break/PDKhan.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool wordBreak(string s, vector<string>& wordDict) {
4+
vector<bool> dp(s.length()+1, false);
5+
6+
dp[0] = true;
7+
8+
for(int i = 1; i <= s.length(); i++){
9+
for(string& word : wordDict){
10+
int len = word.length();
11+
if(i - len >= 0 && s.substr(i-len, len) == word)
12+
dp[i] = dp[i - len];
13+
14+
if(dp[i])
15+
break;
16+
}
17+
}
18+
19+
return dp[s.length()];
20+
}
21+
};

0 commit comments

Comments
 (0)