Skip to content

Commit 4c7af5b

Browse files
committed
🚀 13-May-2022
1 parent 753645b commit 4c7af5b

36 files changed

+2990
-246
lines changed
Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
1+
Given a string S of lowercase letters, a duplicate removal consists of
2+
choosing two adjacent and equal letters, and removing them.
23

34
We repeatedly make duplicate removals on S until we no longer can.
45

5-
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
6+
Return the final string after all such duplicate removals have been made.
7+
It is guaranteed the answer is unique.
68

79

810

@@ -11,7 +13,9 @@ Example 1:
1113
Input: "abbaca"
1214
Output: "ca"
1315
Explanation:
14-
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
16+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal,
17+
and this is the only possible move. The result of this move is that the string is "aaca",
18+
of which only "aa" is possible, so the final string is "ca".
1519

1620

1721
Note:
@@ -28,38 +32,85 @@ S consists only of English lowercase letters.
2832

2933

3034

31-
35+
// TC -> O(n)
36+
// SC -> O(n)
3237

3338
class Solution {
34-
public:
39+
public:
3540
string removeDuplicates(string S) {
3641
stack<char> s;
37-
int n=S.length();
42+
int n = S.length();
3843
s.push(S[0]);
39-
for(int i=1;i<n;i++){
40-
if(!s.empty() && s.top()==S[i])
44+
for (int i = 1; i < n; i++) {
45+
if (!s.empty() && s.top() == S[i])
4146
s.pop();
42-
else s.push(S[i]);
47+
else
48+
s.push(S[i]);
4349
}
44-
string res="";
50+
string res = "";
4551

46-
while(!s.empty()){
47-
res=s.top()+res;
52+
while (!s.empty()) {
53+
res = s.top() + res;
4854
s.pop();
4955
}
5056
return res;
5157
}
5258
};
5359

5460

61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
// TC -> O(n)
71+
// SC -> O(n)
72+
5573
class Solution {
5674
public:
75+
string removeDuplicates(string s) {
76+
int n = s.length();
77+
string tmp = "";
78+
79+
for (int i = 0; i < n; i++) {
80+
if (tmp.length() == 0) tmp += s[i];
81+
else if (tmp[tmp.length() - 1] != s[i]) tmp += s[i];
82+
else tmp.erase(tmp.length() - 1);
83+
}
84+
85+
return tmp;
86+
}
87+
};
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
// TC -> O(n)
103+
// SC -> O(1)
104+
105+
class Solution {
106+
public:
57107
string removeDuplicates(string S) {
58-
int k=0, n=S.length();
59-
for(int i=0;i<n;i++, k++){
60-
S[k]=S[i];
61-
if(k>0 && S[k-1]==S[k]) k-=2;
108+
int k = 0, n = S.length();
109+
for (int i = 0; i < n; i++, k++) {
110+
S[k] = S[i];
111+
if (k > 0 && S[k - 1] == S[k]) k -= 2;
62112
}
63113
return S.substr(0, k);
64114
}
65115
};
116+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Given a binary tree
2+
3+
struct Node {
4+
int val;
5+
Node *left;
6+
Node *right;
7+
Node *next;
8+
}
9+
Populate each next pointer to point to its next right node.
10+
If there is no next right node, the next pointer should be set to NULL.
11+
12+
Initially, all next pointers are set to NULL.
13+
14+
15+
16+
Example 1:
17+
18+
19+
Input: root = [1,2,3,4,5,null,7]
20+
Output: [1,#,2,3,#,4,5,7,#]
21+
Explanation: Given the above binary tree (Figure A), your function should
22+
populate each next pointer to point to its next right node, just like in Figure B.
23+
The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
24+
Example 2:
25+
26+
Input: root = []
27+
Output: []
28+
29+
30+
Constraints:
31+
32+
The number of nodes in the tree is in the range [0, 6000].
33+
-100 <= Node.val <= 100
34+
35+
36+
Follow-up:
37+
38+
You may only use constant extra space.
39+
The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
40+
41+
42+
43+
44+
45+
46+
47+
48+
/*
49+
// Definition for a Node.
50+
class Node {
51+
public:
52+
int val;
53+
Node* left;
54+
Node* right;
55+
Node* next;
56+
57+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
58+
59+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
60+
61+
Node(int _val, Node* _left, Node* _right, Node* _next)
62+
: val(_val), left(_left), right(_right), next(_next) {}
63+
};
64+
*/
65+
66+
class Solution {
67+
public:
68+
Node* connect(Node* root) {
69+
if (!root) return root;
70+
queue <Node*> q;
71+
q.push(root);
72+
73+
while (!q.empty()) {
74+
int size = q.size();
75+
while (size--) {
76+
Node *curr = q.front();
77+
q.pop();
78+
79+
if (size == 0) curr->next = NULL;
80+
else curr->next = q.front();
81+
82+
if (curr->left) q.push(curr->left);
83+
if (curr->right) q.push(curr->right);
84+
}
85+
}
86+
return root;
87+
}
88+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
You are given a string s, and an array of pairs of indices in the string pairs
2+
where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.
3+
4+
You can swap the characters at any pair of indices in the given pairs any number of times.
5+
6+
Return the lexicographically smallest string that s can be changed to after using the swaps.
7+
8+
9+
10+
Example 1:
11+
12+
Input: s = "dcab", pairs = [[0,3],[1,2]]
13+
Output: "bacd"
14+
Explaination:
15+
Swap s[0] and s[3], s = "bcad"
16+
Swap s[1] and s[2], s = "bacd"
17+
Example 2:
18+
19+
Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
20+
Output: "abcd"
21+
Explaination:
22+
Swap s[0] and s[3], s = "bcad"
23+
Swap s[0] and s[2], s = "acbd"
24+
Swap s[1] and s[2], s = "abcd"
25+
Example 3:
26+
27+
Input: s = "cba", pairs = [[0,1],[1,2]]
28+
Output: "abc"
29+
Explaination:
30+
Swap s[0] and s[1], s = "bca"
31+
Swap s[1] and s[2], s = "bac"
32+
Swap s[0] and s[1], s = "abc"
33+
34+
35+
Constraints:
36+
37+
1 <= s.length <= 10^5
38+
0 <= pairs.length <= 10^5
39+
0 <= pairs[i][0], pairs[i][1] < s.length
40+
s only contains lower case English letters.
41+
42+
43+
44+
45+
46+
47+
48+
49+
#define MAX 100000
50+
51+
class Solution {
52+
public:
53+
vector <int> adj[MAX];
54+
vector <bool> vis;
55+
void dfs(int src, vector <char> &characters, vector <int> &indices, string &s) {
56+
vis[src] = true;
57+
characters.push_back(s[src]);
58+
indices.push_back(src);
59+
60+
for (auto &dst: adj[src]) {
61+
if (!vis[dst]){
62+
dfs(dst, characters, indices, s);
63+
}
64+
}
65+
}
66+
67+
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
68+
int n = s.length();
69+
vis.resize(n, false);
70+
71+
for (auto &pair: pairs) {
72+
adj[pair[0]].push_back(pair[1]);
73+
adj[pair[1]].push_back(pair[0]);
74+
}
75+
76+
for (int i = 0; i < n; i++) {
77+
if (!vis[i]) {
78+
vector <char> characters;
79+
vector <int> indices;
80+
81+
dfs(i, characters, indices, s);
82+
83+
sort(characters.begin(), characters.end());
84+
sort(indices.begin(), indices.end());
85+
86+
for (int index = 0; index < indices.size(); index++) {
87+
s[indices[index]] = characters[index];
88+
}
89+
}
90+
}
91+
return s;
92+
}
93+
};
94+
95+
96+
// TC: O(VlogV + E)
97+
// SC: O(V + E)

0 commit comments

Comments
 (0)