Skip to content

Commit 025a546

Browse files
author
kaidul
committed
646 problems solved
1 parent 4468b04 commit 025a546

33 files changed

+1798
-587
lines changed

README.md

+619-587
Large diffs are not rendered by default.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
string wordAfterTyping(string word) {
3+
stack<char> Stack;
4+
for(char ch : word) {
5+
if(ch == '#') {
6+
if(!Stack.empty()) {
7+
Stack.pop();
8+
}
9+
} else {
10+
Stack.push(ch);
11+
}
12+
}
13+
string result = "";
14+
while(!Stack.empty()) {
15+
result += Stack.top();
16+
Stack.pop();
17+
}
18+
19+
reverse(result.begin(), result.end());
20+
21+
return result;
22+
}
23+
public:
24+
bool backspaceCompare(string S, string T) {
25+
return wordAfterTyping(S) == wordAfterTyping(T);
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
pair<int, int> longestConsecutive(TreeNode* root, int& longest) {
12+
if (!root->left and !root->right) {
13+
longest = max(longest, 1);
14+
return {1, 1};
15+
}
16+
int incrSeqLeftLen = 1, decSeqLeftLen = 1, incrSeqRightLen = 1, decSeqRightLen = 1;
17+
if (root->left) {
18+
pair<int, int> leftSeqLen = longestConsecutive(root->left, longest);
19+
if (root->left->val + 1 == root->val) {
20+
incrSeqLeftLen = max(incrSeqLeftLen, leftSeqLen.first + 1);
21+
} else if (root->left->val == root->val + 1) {
22+
decSeqLeftLen = max(decSeqLeftLen, leftSeqLen.second + 1);
23+
}
24+
}
25+
26+
if (root->right) {
27+
pair<int, int> rightSeqLen = longestConsecutive(root->right, longest);
28+
if (root->right->val + 1 == root->val) {
29+
incrSeqRightLen = max(incrSeqRightLen, rightSeqLen.first + 1);
30+
} else if (root->right->val == root->val + 1) {
31+
decSeqRightLen = max(decSeqRightLen, rightSeqLen.second + 1);
32+
}
33+
}
34+
35+
longest = max(longest, max(incrSeqLeftLen + decSeqRightLen - 1, decSeqLeftLen + incrSeqRightLen - 1));
36+
37+
return {max(incrSeqLeftLen, incrSeqRightLen), max(decSeqLeftLen, decSeqRightLen)};
38+
}
39+
public:
40+
int longestConsecutive(TreeNode* root) {
41+
if (!root) return 0;
42+
int longest = INT_MIN;
43+
longestConsecutive(root, longest);
44+
45+
return longest;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
9+
Node() {}
10+
11+
Node(int _val, Node* _left, Node* _right) {
12+
val = _val;
13+
left = _left;
14+
right = _right;
15+
}
16+
};
17+
*/
18+
class Solution {
19+
void iterateBstLeft(Node* root, stack<Node*>& nodeStack) {
20+
while (root) {
21+
nodeStack.push(root);
22+
root = root->left;
23+
}
24+
}
25+
26+
void treeToDoublyList(stack<Node*>& nodeStack, Node*& head, Node*& tail) {
27+
if (nodeStack.empty()) {
28+
return;
29+
}
30+
Node* currNode = nodeStack.top();
31+
nodeStack.pop();
32+
iterateBstLeft(currNode->right, nodeStack);
33+
currNode->left = currNode->right = nullptr;
34+
if (!head) {
35+
head = tail = currNode;
36+
} else {
37+
tail->right = currNode;
38+
currNode->left = tail;
39+
tail = currNode;
40+
}
41+
treeToDoublyList(nodeStack, head, tail);
42+
}
43+
44+
public:
45+
Node* treeToDoublyList(Node* root) {
46+
stack<Node*> nodeStack;
47+
Node* head = nullptr;
48+
Node* tail = nullptr;
49+
50+
iterateBstLeft(root, nodeStack);
51+
treeToDoublyList(nodeStack, head, tail);
52+
if (head) {
53+
tail->right = head;
54+
head->left = tail;
55+
}
56+
57+
return head;
58+
}
59+
};

source-code/Custom_Sort_String.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string customSortString(string S, string T) {
4+
vector<int> order(30, -1);
5+
vector<int> letter(30);
6+
for (int i = 0; i < (int) S.length(); i++) {
7+
order[S[i] - 'a'] = i;
8+
letter[i] = S[i] - 'a';
9+
}
10+
string result = "";
11+
vector<int> count(30, 0);
12+
for (int i = 0; i < (int)T.length(); i++) {
13+
if(order[T[i] - 'a'] != -1) {
14+
count[order[T[i] - 'a']]++;
15+
} else {
16+
result += T[i];
17+
}
18+
}
19+
for (int i = 0; i < 26; i++) {
20+
while (count[i]--) {
21+
result += ('a' + letter[i]);
22+
}
23+
}
24+
25+
return result;
26+
}
27+
};

source-code/DI_String_Match.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> diStringMatch(string S) {
4+
int n = (int)S.length();
5+
int low = 0, high = n;
6+
vector<int> result;
7+
for (int i = 0; i < n; i++) {
8+
result.push_back(S[i] == 'D' ? high-- : low++);
9+
}
10+
assert(low == high);
11+
result.push_back(high); // or low
12+
13+
return result;
14+
}
15+
};

source-code/Design_Circular_Queue.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class MyCircularQueue {
2+
int frontIndx;
3+
int rearIndx;
4+
int maxSize;
5+
vector<int> container;
6+
int getSize() {
7+
return rearIndx - frontIndx + 1;
8+
}
9+
public:
10+
/** Initialize your data structure here. Set the size of the queue to be k. */
11+
MyCircularQueue(int k) {
12+
container = vector<int>(k);
13+
this->frontIndx = 0;
14+
this->rearIndx = -1;
15+
this->maxSize = k;
16+
}
17+
18+
/** Insert an element into the circular queue. Return true if the operation is successful. */
19+
bool enQueue(int value) {
20+
if(isFull()) {
21+
return false;
22+
}
23+
container[++rearIndx % maxSize] = value;
24+
return true;
25+
}
26+
27+
/** Delete an element from the circular queue. Return true if the operation is successful. */
28+
bool deQueue() {
29+
if(isEmpty()) {
30+
return false;
31+
}
32+
frontIndx++;
33+
return true;
34+
}
35+
36+
/** Get the front item from the queue. */
37+
int Front() {
38+
if(isEmpty()) {
39+
return -1;
40+
}
41+
return container[frontIndx % maxSize];
42+
}
43+
44+
/** Get the last item from the queue. */
45+
int Rear() {
46+
if(isEmpty()) {
47+
return -1;
48+
}
49+
return container[rearIndx % maxSize];
50+
}
51+
52+
/** Checks whether the circular queue is empty or not. */
53+
bool isEmpty() {
54+
return getSize() == 0;
55+
}
56+
57+
/** Checks whether the circular queue is full or not. */
58+
bool isFull() {
59+
return getSize() == maxSize;
60+
}
61+
};
62+
63+
/**
64+
* Your MyCircularQueue object will be instantiated and called as such:
65+
* MyCircularQueue obj = new MyCircularQueue(k);
66+
* bool param_1 = obj.enQueue(value);
67+
* bool param_2 = obj.deQueue();
68+
* int param_3 = obj.Front();
69+
* int param_4 = obj.Rear();
70+
* bool param_5 = obj.isEmpty();
71+
* bool param_6 = obj.isFull();
72+
*/

source-code/Design_HashMap.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class MyHashMap {
2+
int[] entry;
3+
4+
/** Initialize your data structure here. */
5+
public MyHashMap() {
6+
entry = new int[1000001];
7+
for(int i = 0; i < entry.length; i++) {
8+
entry[i] = -1;
9+
}
10+
}
11+
12+
/** value will always be non-negative. */
13+
public void put(int key, int value) {
14+
entry[key] = value;
15+
}
16+
17+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
18+
public int get(int key) {
19+
return entry[key];
20+
}
21+
22+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
23+
public void remove(int key) {
24+
entry[key] = -1;
25+
}
26+
}
27+
28+
/**
29+
* Your MyHashMap object will be instantiated and called as such:
30+
* MyHashMap obj = new MyHashMap();
31+
* obj.put(key,value);
32+
* int param_2 = obj.get(key);
33+
* obj.remove(key);
34+
*/

source-code/Find_Anagram_Mappings.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
4+
int n = (int) A.size();
5+
vector<int> result(n);
6+
unordered_map<int, vector<int>> index;
7+
for (int i = 0; i < n; i++) {
8+
index[A[i]].push_back(i);
9+
}
10+
for (int i = 0; i < n; i++) {
11+
int indx = index[B[i]].back();
12+
result[indx] = i;
13+
index[B[i]].pop_back();
14+
}
15+
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)