class Solution {
public:
int isPrefixOfWord(string sentence, string searchWord) {
int len = sentence.length();
if (sentence[len-1] != ' ') {
sentence += " ";
len++;
}
int last = 0;
vector<string> words;
for (int i = 1 ; i< len ; i++) {
if (sentence[i] == ' ') {
words.push_back(sentence.substr(last, i-last));
last = i + 1;
}
}
for (int i = 0 ; i < (int)words.size() ; i++) {
int lv = words[i].length(), ls = searchWord.length();
if (lv < ls) continue;
if (words[i].substr(0, ls) == searchWord) return i+1;
}
return -1;
}
};
class Solution {
public:
int maxVowels(string s, int k) {
int len = (int)s.length(), cnt = 0, ans = 0;
for (int i = 0 ; i < k ; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
cnt++;
}
}
ans = cnt;
for (int i = k ; i < len ; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
cnt++;
}
if (s[i-k] == 'a' || s[i-k] == 'e' || s[i-k] == 'i' || s[i-k] == 'o' || s[i-k] == 'u') {
cnt--;
}
ans = max(ans, cnt);
}
return ans;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void dfs(TreeNode* root, vector<int>& cnt, int& ans) {
if (root->left == NULL && root->right == NULL) {
int sum = 0, odd = 0;
for (int i = 1 ; i <= 9 ; i++) {
if (cnt[i] % 2) odd++;
sum += cnt[i];
}
if (sum % 2 == odd) ans++;
return;
}
if (root->left) {
cnt[root->left->val]++;
dfs(root->left, cnt, ans);
cnt[root->left->val]--;
}
if (root->right) {
cnt[root->right->val]++;
dfs(root->right, cnt, ans);
cnt[root->right->val]--;
}
}
int pseudoPalindromicPaths (TreeNode* root) {
if (root == NULL) return 0;
vector<int> cnt = vector<int>(10, 0);
cnt[root->val]++;
int ans = 0;
dfs(root, cnt, ans);
cnt[root->val]--;
return ans;
}
};
class Solution {
public:
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
int n1 = (int)nums1.size(), n2 = (int)nums2.size(), len = min(n1, n2);
vector<vector<int>> dp = vector<vector<int>>(n1+1, vector<int>(n2+1, -50000000));
int ans = INT_MIN;
for (int i = 0 ; i <= n1 ; i++) {
dp[i][0] = 0;
}
for (int i = 0 ; i <= n2 ; i++) {
dp[0][i] = 0;
}
for (int j = 1 ; j <= n1 ; j++) {
for (int k = 1 ; k <= n2 ; k++) {
if (j > 1 && k > 1) {
dp[j][k] = dp[j-1][k-1];
}
if (j > 1) {
dp[j][k] = max(dp[j][k], dp[j-1][k]);
}
if (k > 1) {
dp[j][k] = max(dp[j][k], dp[j][k-1]);
}
dp[j][k] = max(dp[j][k], max(dp[j-1][k-1] + nums1[j-1] * nums2[k-1], nums1[j-1] * nums2[k-1]));
// cout << j << "<>" << k << "==" << dp[j][k] << " | " << ans << endl;
ans = max(ans, dp[j][k]);
}
}
return ans;
}
};