Skip to content

Commit 6b21e4c

Browse files
committed
🚀 11-Aug-2020
1 parent 83dfe3a commit 6b21e4c

14 files changed

Lines changed: 710 additions & 30 deletions

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
temporary/
2-
Notes.md
2+
Notes.md
3+
.cph
4+
.vscode
5+
Personal.cpp
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
2+
3+
Given a balanced string s split it in the maximum amount of balanced strings.
4+
5+
Return the maximum amount of splitted balanced strings.
6+
7+
8+
9+
Example 1:
10+
11+
Input: s = "RLRRLLRLRL"
12+
Output: 4
13+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
14+
Example 2:
15+
16+
Input: s = "RLLLLRRRLR"
17+
Output: 3
18+
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
19+
Example 3:
20+
21+
Input: s = "LLLLRRRR"
22+
Output: 1
23+
Explanation: s can be split into "LLLLRRRR".
24+
Example 4:
25+
26+
Input: s = "RLRRRLLRLL"
27+
Output: 2
28+
Explanation: s can be split into "RL", "RRRLLRLL", since each substring contains an equal number of 'L' and 'R'
29+
30+
31+
Constraints:
32+
33+
1 <= s.length <= 1000
34+
s[i] = 'L' or 'R'
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
class Solution {
47+
public:
48+
int balancedStringSplit(string s) {
49+
int n=s.length();
50+
int cnt=0, tmp=0;
51+
for(int i=0;i<n;i++){
52+
if(s[i]=='L') tmp-=1;
53+
else if(s[i]=='R') tmp+=1;
54+
if(tmp==0) cnt++;
55+
}
56+
return cnt;
57+
}
58+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:
2+
3+
Characters ('a' to 'i') are represented by ('1' to '9') respectively.
4+
Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
5+
Return the string formed after mapping.
6+
7+
It's guaranteed that a unique mapping will always exist.
8+
9+
10+
11+
Example 1:
12+
13+
Input: s = "10#11#12"
14+
Output: "jkab"
15+
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
16+
Example 2:
17+
18+
Input: s = "1326#"
19+
Output: "acz"
20+
Example 3:
21+
22+
Input: s = "25#"
23+
Output: "y"
24+
Example 4:
25+
26+
Input: s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
27+
Output: "abcdefghijklmnopqrstuvwxyz"
28+
29+
30+
Constraints:
31+
32+
1 <= s.length <= 1000
33+
s[i] only contains digits letters ('0'-'9') and '#' letter.
34+
s will be valid string such that mapping is always possible.
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
class Solution {
45+
public:
46+
string freqAlphabets(string s) {
47+
string res;
48+
int n=s.length();
49+
for(int i=n-1;i>=0;i--){
50+
if(s[i]=='#'){
51+
int index=(s[i-2]-'0')*10 + s[i-1]-'0';
52+
res+=char('a'-1+index);
53+
i-=2; // moving two steps
54+
} else{
55+
res+=char('a'-1+s[i]-'0'); // -1 as 'a' starts from 1 and not zero
56+
}
57+
}
58+
reverse(res.begin(), res.end());
59+
return res;
60+
}
61+
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Given a string s. You should re-order the string using the following algorithm:
2+
3+
Pick the smallest character from s and append it to the result.
4+
Pick the smallest character from s which is greater than the last appended character to the result and append it.
5+
Repeat step 2 until you cannot pick more characters.
6+
Pick the largest character from s and append it to the result.
7+
Pick the largest character from s which is smaller than the last appended character to the result and append it.
8+
Repeat step 5 until you cannot pick more characters.
9+
Repeat the steps from 1 to 6 until you pick all characters from s.
10+
In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
11+
12+
Return the result string after sorting s with this algorithm.
13+
14+
15+
16+
Example 1:
17+
18+
Input: s = "aaaabbbbcccc"
19+
Output: "abccbaabccba"
20+
Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
21+
After steps 4, 5 and 6 of the first iteration, result = "abccba"
22+
First iteration is done. Now s = "aabbcc" and we go back to step 1
23+
After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
24+
After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
25+
Example 2:
26+
27+
Input: s = "rat"
28+
Output: "art"
29+
Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.
30+
Example 3:
31+
32+
Input: s = "leetcode"
33+
Output: "cdelotee"
34+
Example 4:
35+
36+
Input: s = "ggggggg"
37+
Output: "ggggggg"
38+
Example 5:
39+
40+
Input: s = "spo"
41+
Output: "ops"
42+
43+
44+
Constraints:
45+
46+
1 <= s.length <= 500
47+
s contains only lower-case English letters.
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
class Solution {
63+
public:
64+
string sortString(string s) {
65+
string res;
66+
int freq[26];
67+
int n=s.length();
68+
fill(freq, freq+26, 0);
69+
70+
for(int i=0;i<n;i++){
71+
freq[s[i]-'a']++;
72+
}
73+
74+
while(n){
75+
for(int i=0;i<26;i++){
76+
if(freq[i]>0){
77+
res+=char('a'+i);
78+
freq[i]--;
79+
n--;
80+
}
81+
}
82+
for(int i=25;i>=0;i--){
83+
if(freq[i]>0){
84+
res+=char('a'+i);
85+
freq[i]--;
86+
n--;
87+
}
88+
}
89+
}
90+
return res;
91+
}
92+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Share
2+
Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.
3+
4+
The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.
5+
6+
7+
8+
Example 1:
9+
10+
Input: n = 4
11+
Output: "pppz"
12+
Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
13+
Example 2:
14+
15+
Input: n = 2
16+
Output: "xy"
17+
Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".
18+
Example 3:
19+
20+
Input: n = 7
21+
Output: "holasss"
22+
23+
24+
Constraints:
25+
26+
1 <= n <= 500
27+
28+
29+
30+
31+
32+
33+
34+
class Solution {
35+
public:
36+
string generateTheString(int n) {
37+
// Just going to use 'a' and 'b' in the string
38+
string res;
39+
if(n%2==0){
40+
for(int i=0;i<n-1;i++)
41+
res+='a';
42+
res+='b';
43+
} else {
44+
for(int i=0;i<n;i++)
45+
res+='a';
46+
}
47+
return res;
48+
}
49+
};
50+
51+
52+
// if n is odd then 'a' will be added odd times
53+
// if n is even then we add 1 'b' and n-1 becomes odd, so we add 'a' n-1 times
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
2+
3+
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
4+
5+
6+
7+
Example 1:
8+
9+
Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
10+
Output: "Sao Paulo"
11+
Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
12+
Example 2:
13+
14+
Input: paths = [["B","C"],["D","B"],["C","A"]]
15+
Output: "A"
16+
Explanation: All possible trips are:
17+
"D" -> "B" -> "C" -> "A".
18+
"B" -> "C" -> "A".
19+
"C" -> "A".
20+
"A".
21+
Clearly the destination city is "A".
22+
Example 3:
23+
24+
Input: paths = [["A","Z"]]
25+
Output: "Z"
26+
27+
28+
Constraints:
29+
30+
1 <= paths.length <= 100
31+
paths[i].length == 2
32+
1 <= cityAi.length, cityBi.length <= 10
33+
cityAi != cityBi
34+
All strings consist of lowercase and uppercase English letters and the space character.
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
class Solution {
47+
public:
48+
string destCity(vector<vector<string>>& paths) {
49+
int n=paths.size();
50+
unordered_map<string, string> mp; // <cityA, cityB>
51+
for(int i=0;i<n;i++){
52+
mp.insert({paths[i][0], paths[i][1]});
53+
}
54+
string src=paths[0][0], dst="";
55+
while(1){
56+
if(mp.find(src)!=mp.end()){
57+
dst=mp[src];
58+
src=dst;
59+
} else break;
60+
}
61+
return dst;
62+
}
63+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
2+
3+
Example 1:
4+
Input: "Let's take LeetCode contest"
5+
Output: "s'teL ekat edoCteeL tsetnoc"
6+
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
7+
8+
9+
10+
11+
12+
13+
14+
15+
class Solution {
16+
public:
17+
string reverseWords(string s) {
18+
int n=s.length();
19+
string res, tmp;
20+
for(int i=0;i<n;i++){
21+
if(s[i]==' '){
22+
res+=tmp+ ' ';
23+
tmp="";
24+
continue;
25+
}
26+
tmp=s[i]+tmp;
27+
}
28+
res+=tmp; // adding tmp again as we don't have space in the end
29+
return res;
30+
}
31+
};
32+
33+
34+
35+
36+
// Alter
37+
// We can also use stack

0 commit comments

Comments
 (0)