2020-12-09 Daily-Challenge
Today I have done Distinct Subsequences and Distinct Subsequences II on leetcode and leetcode's December LeetCoding Challenge with cpp
.
Distinct Subsequences
Description
Given two strings s
and t
, return the number of distinct subsequences of s
which equals t
.
A string's subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ACE"
is a subsequence of "ABCDE"
while "AEC"
is not).
It's guaranteed the answer fits on a 32-bit signed integer.
Example 1:
Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from S.
rabbbit
rabbbit
rabbbit
Example 2:
Input: s = "babgbag", t = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
babgbag
babgbag
babgbag
babgbag
babgbag
Constraints:
0 <= s.length, t.length <= 1000
s
andt
consist of English letters.
Solution
LCS DP, but I forgot it... fuck
class Solution {
public:
int numDistinct(string s, string t) {
int slen = s.length();
int tlen = t.length();
if(slen <= tlen) return s == t;
vector<vector<long long>> dp(slen+1, vector<long long>(tlen+1));
for(int i = 0; i <= slen; ++i) {
dp[i][0] = 1;
}
for(int i = 1; i <= slen; ++i) {
for(int j = max(1, tlen+i-slen-1); j <= min(slen-tlen+i, tlen); ++j) {
dp[i][j] = dp[i-1][j];
if(s[i-1] == t[j-1]) dp[i][j] += dp[i-1][j-1];
}
}
return dp.back().back();
}
};
Distinct Subsequences II
Description
Given a string S
, count the number of distinct, non-empty subsequences of S
.
Since the result may be large, return the answer modulo 10^9 + 7
.
Example 1:
Input: "abc"
Output: 7
Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
Example 2:
Input: "aba"
Output: 6
Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba".
Example 3:
Input: "aaa"
Output: 3
Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".
Note:
S
contains only lowercase letters.1 <= S.length <= 2000
Solution
little thing similar to previous problem, check solution for more information.
class Solution {
public:
int distinctSubseqII(string S) {
const int MOD = 1000000007;
int len = S.length();
vector<int> dp(len+1);
dp[0] = 1;
vector<int> last(26, -1);
for(int i = 1; i <= len; ++i) {
dp[i] = dp[i-1] * 2 % MOD;
int c = S[i-1] - 'a';
if(last[c] != -1) {
dp[i] -= dp[last[c]];
}
dp[i] = (dp[i] + MOD) % MOD;
last[c] = i-1;
}
dp[len] = (dp[len] - 1 + MOD) % MOD;
return dp.back();
}
};
December LeetCoding Challenge 9
Description
Binary Search Tree Iterator
Implement the BSTIterator
class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root)
Initializes an object of theBSTIterator
class. Theroot
of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.boolean hasNext()
Returnstrue
if there exists a number in the traversal to the right of the pointer, otherwise returnsfalse
.int next()
Moves the pointer to the right, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next()
will return the smallest element in the BST.
You may assume that next()
calls will always be valid. That is, there will be at least a next number in the in-order traversal when next()
is called.
Example 1:
Input
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output
[null, 3, 7, true, 9, true, 15, true, 20, false]
Explanation
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False
Constraints:
- The number of nodes in the tree is in the range
[1, 105]
. 0 <= Node.val <= 106
- At most
105
calls will be made tohasNext
, andnext
.
Follow up:
- Could you implement
next()
andhasNext()
to run in averageO(1)
time and useO(h)
memory, whereh
is the height of the tree?
Solution
nothing to say
class BSTIterator {
vector<TreeNode*> previous;
TreeNode* cur;
public:
BSTIterator(TreeNode* root) {
while(root && root->left) {
previous.push_back(root);
root = root->left;
}
cur = root;
}
int next() {
int result = cur->val;
if(cur->right) {
TreeNode *root = cur->right;
while(root && root->left) {
previous.push_back(root);
root = root->left;
}
cur = root;
} else {
if(previous.size()) {
cur = previous.back();
previous.pop_back();
} else {
cur = nullptr;
}
}
return result;
}
bool hasNext() {
return cur;
}
};