Skip to content
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
31 changes: 31 additions & 0 deletions clone-graph/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
void dfs(Node* node, unordered_map<Node*, Node*>& map){
if(map.count(node))
return;

map[node] = new Node(node->val);

for(int i = 0; i < node->neighbors.size(); i++)
dfs(node->neighbors[i], map);
}

Node* cloneGraph(Node* node) {
if(node == NULL)
return NULL;

unordered_map<Node*, Node*> map;

dfs(node, map);

for(auto& x : map){
Node* org = x.first;
Node* dst = x.second;
for(int i = 0; i < org->neighbors.size(); i++){
dst->neighbors.push_back(map[org->neighbors[i]]);
}
}

return map[node];
}
};
19 changes: 19 additions & 0 deletions longest-common-subsequence/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int t1_len = text1.length();
int t2_len = text2.length();
vector<vector<int>> dp(t1_len + 1, vector(t2_len + 1, 0));

for(int i = 1; i <= t1_len; i++){
for(int j = 1; j <= t2_len; j++){
if(text1[i-1] == text2[j-1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}

return dp[t1_len][t2_len];
}
};
24 changes: 24 additions & 0 deletions longest-repeating-character-replacement/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int characterReplacement(string s, int k) {
int start = 0;
int max_len = 0;
int max_cnt = 0;
int map[26] = {0};

for(int end = 0; end < s.length(); end++){
map[s[end] - 'A']++;

max_cnt = max(max_cnt, map[s[end] - 'A']);

while(end - start + 1 - max_cnt > k){
map[s[start] - 'A']--;
start++;
}

max_len = max(max_len, end - start + 1);
}

return max_len;
}
};
28 changes: 28 additions & 0 deletions palindromic-substrings/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int countSubstrings(string s) {
int cnt = 0;

for(int i = 0; i < s.length(); i++){
int start = i;
int end = i;

while(0 <= start && end < s.length() && s[start] == s[end]){
cnt++;
start--;
end++;
}

start = i;
end = i + 1;

while(0 <= start && end < s.length() && s[start] == s[end]){
cnt++;
start--;
end++;
}
}

return cnt;
}
};
15 changes: 15 additions & 0 deletions reverse-bits/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t result = 0;

for(int i = 0; i < 32; i++){
result <<= 1;

result |= (n & 1);
n >>= 1;
}

return result;
}
};