Skip to content

Commit 48bc1fb

Browse files
committed
🚀 14-Aug-2020
1 parent ece9b54 commit 48bc1fb

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Design an Iterator class, which has:
2+
3+
A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
4+
A function next() that returns the next combination of length combinationLength in lexicographical order.
5+
A function hasNext() that returns True if and only if there exists a next combination.
6+
7+
8+
Example:
9+
10+
CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.
11+
12+
iterator.next(); // returns "ab"
13+
iterator.hasNext(); // returns true
14+
iterator.next(); // returns "ac"
15+
iterator.hasNext(); // returns true
16+
iterator.next(); // returns "bc"
17+
iterator.hasNext(); // returns false
18+
19+
20+
Constraints:
21+
22+
1 <= combinationLength <= characters.length <= 15
23+
There will be at most 10^4 function calls per test.
24+
It's guaranteed that all calls of the function next are valid.
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class CombinationIterator {
38+
public:
39+
queue<string> q;
40+
41+
void allCombinations(string characters, int k, int s, string str){
42+
if(k==0){
43+
q.push(str);
44+
return;
45+
}
46+
for(int i=s; i<characters.length();i++){
47+
str+=characters[i];
48+
allCombinations(characters, k-1, i+1, str);
49+
str.erase(str.end()-1);
50+
}
51+
}
52+
53+
CombinationIterator(string characters, int combinationLength) {
54+
string str="";
55+
allCombinations(characters, combinationLength, 0, str); // <characters, combinationLength, start, str>
56+
}
57+
58+
string next() {
59+
string next = q.front();
60+
q.pop();
61+
return next;
62+
}
63+
64+
bool hasNext() {
65+
return !q.empty();
66+
}
67+
};
68+
69+
/**
70+
* Your CombinationIterator object will be instantiated and called as such:
71+
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
72+
* string param_1 = obj->next();
73+
* bool param_2 = obj->hasNext();
74+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Design an Iterator class, which has:
2+
3+
A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
4+
A function next() that returns the next combination of length combinationLength in lexicographical order.
5+
A function hasNext() that returns True if and only if there exists a next combination.
6+
7+
8+
Example:
9+
10+
CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.
11+
12+
iterator.next(); // returns "ab"
13+
iterator.hasNext(); // returns true
14+
iterator.next(); // returns "ac"
15+
iterator.hasNext(); // returns true
16+
iterator.next(); // returns "bc"
17+
iterator.hasNext(); // returns false
18+
19+
20+
Constraints:
21+
22+
1 <= combinationLength <= characters.length <= 15
23+
There will be at most 10^4 function calls per test.
24+
It's guaranteed that all calls of the function next are valid.
25+
Hide Hint #1
26+
Generate all combinations as a preprocessing.
27+
Hide Hint #2
28+
Use bit masking to generate all the combinations.
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
class CombinationIterator {
42+
public:
43+
queue<string> q;
44+
45+
void allCombinations(string characters, int k, int s, string str){
46+
if(k==0){
47+
q.push(str);
48+
return;
49+
}
50+
for(int i=s; i<characters.length();i++){
51+
str+=characters[i];
52+
allCombinations(characters, k-1, i+1, str);
53+
str.erase(str.end()-1);
54+
}
55+
}
56+
57+
CombinationIterator(string characters, int combinationLength) {
58+
string str="";
59+
allCombinations(characters, combinationLength, 0, str); // <characters, combinationLength, start, str>
60+
}
61+
62+
string next() {
63+
string next = q.front();
64+
q.pop();
65+
return next;
66+
}
67+
68+
bool hasNext() {
69+
return !q.empty();
70+
}
71+
};
72+
73+
/**
74+
* Your CombinationIterator object will be instantiated and called as such:
75+
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
76+
* string param_1 = obj->next();
77+
* bool param_2 = obj->hasNext();
78+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
2+
3+
This is case sensitive, for example "Aa" is not considered a palindrome here.
4+
5+
Note:
6+
Assume the length of given string will not exceed 1,010.
7+
8+
Example:
9+
10+
Input:
11+
"abccccdd"
12+
13+
Output:
14+
7
15+
16+
Explanation:
17+
One longest palindrome that can be built is "dccaccd", whose length is 7.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
int longestPalindrome(string s) {
34+
int n=s.length();
35+
map<char, int> freq;
36+
for(int i=0;i<n;i++)
37+
freq[s[i]]++;
38+
int res=0;
39+
for(auto it=freq.begin(); it!=freq.end(); it++){
40+
if(it->second>0){
41+
if(it->second%2==0){
42+
res+=it->second;
43+
it->second=0;
44+
} else {
45+
res+=it->second-1;
46+
it->second=1;
47+
}
48+
}
49+
}
50+
for(auto it=freq.begin(); it!=freq.end(); it++){
51+
if(it->second > 0){
52+
res+=1;
53+
break;
54+
}
55+
}
56+
return res;
57+
}
58+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
2+
3+
This is case sensitive, for example "Aa" is not considered a palindrome here.
4+
5+
Note:
6+
Assume the length of given string will not exceed 1,010.
7+
8+
Example:
9+
10+
Input:
11+
"abccccdd"
12+
13+
Output:
14+
7
15+
16+
Explanation:
17+
One longest palindrome that can be built is "dccaccd", whose length is 7.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
int longestPalindrome(string s) {
34+
int n=s.length();
35+
map<char, int> freq;
36+
for(int i=0;i<n;i++)
37+
freq[s[i]]++;
38+
int res=0;
39+
for(auto it=freq.begin(); it!=freq.end(); it++){
40+
if(it->second>0){
41+
if(it->second%2==0){
42+
res+=it->second;
43+
it->second=0;
44+
} else {
45+
res+=it->second-1;
46+
it->second=1;
47+
}
48+
}
49+
}
50+
for(auto it=freq.begin(); it!=freq.end(); it++){
51+
if(it->second > 0){
52+
res+=1;
53+
break;
54+
}
55+
}
56+
return res;
57+
}
58+
};

dynamic programming/subset_count_with_given_difference.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
Given an array, we have to find the number of subsets with given difference.
3+
example divide the given array into 2 subsets such that S1 - S2 = diff
4+
where S1 = Sum of subset 1 and S2 = Sum of subset 2
5+
6+
input:
7+
arr: 1 1 2 3
8+
diff: 1
9+
10+
output:
11+
3
12+
*/
13+
14+
15+
116
#include<bits/stdc++.h>
217
using namespace std;
318

@@ -45,3 +60,9 @@ int main(){
4560
}
4661
return 0;
4762
}
63+
64+
65+
66+
// We have to find count of all S1 - S2 = diff
67+
// We know S1 + S2 = S
68+
// So S1 = (diff + S) / 2

0 commit comments

Comments
 (0)