# Daily Coding Challenge #14

Daily Coding Challenge (58 Part Series)

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

/*
LeetCode - Check If a Word Occurs As a Prefix of Any Word in a Sentence

Given a sentence that consists of some words separated by a single space, and a searchWord.

You have to check if searchWord is a prefix of any word in sentence.

Return the index of the word in sentence where searchWord is a prefix of this word (1-indexed).

If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.

A prefix of a string S is any leading contiguous substring of S.

Example 1:

Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
Example 2:

Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
Example 3:

Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.
Example 4:

Input: sentence = "i use triple pillow", searchWord = "pill"
Output: 4
Example 5:

Input: sentence = "hello from the other side", searchWord = "they"
Output: -1

Constraints:

1 <= sentence.length <= 100
1 <= searchWord.length <= 10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.
*/

class Solution {
public:
int isPrefixOfWord(string sentence, string searchWord) {
stringstream ss(sentence);
string word;
int ans=1;
while(ss>>word){
// use substr to check if the word starts with searchWord
if(word.substr(0,searchWord.size())==searchWord) return ans;
ans++;
}
return -1;
}
};

class Solution2 {
public:
int isPrefixOfWord(string sentence, string searchWord) {
stringstream ss(sentence);
string word;
int ans=0,cnt;
while(ss>>word){
ans++;
if(word.size()<searchWord.size()) continue;
cnt=0;
for(int i=0;i<searchWord.size();i++){
if(word[i]==searchWord[i]){
cnt++;
}
}
if(cnt==searchWord.size()) {
return ans;
}
}
return -1;
}
};


/*
LeetCode - Maximum Number of Vowels in a Substring of Given Length

Given a string s and an integer k.

Return the maximum number of vowel letters in any substring of s with length k.

Vowel letters in English are (a, e, i, o, u).

Example 1:

Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.
Example 2:

Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.
Example 3:

Input: s = "leetcode", k = 3
Output: 2
Explanation: "lee", "eet" and "ode" contain 2 vowels.
Example 4:

Input: s = "rhythms", k = 4
Output: 0
Explanation: We can see that s doesn't have any vowel letters.
Example 5:

Input: s = "tryhard", k = 4
Output: 1

Constraints:

1 <= s.length <= 10^5
s consists of lowercase English letters.
1 <= k <= s.length
*/

class Solution {
public:
int maxVowels(string s, int k) {
int v={0};
int ans=0,cur=0;
v['a'-'a']=1;
v['e'-'a']=1;
v['i'-'a']=1;
v['o'-'a']=1;
v['u'-'a']=1;
// sliding window approach
// similar to Solution2
for(int i=0;i<s.size();i++){
cur+=v[s[i]-'a'];
// if it s out of boundary, undo the (i-k)th element
if(i>=k) cur-=v[s[i-k]-'a'];
ans=max(ans,cur);
}
return ans;
}
};

class Solution2 {
public:
bool isVowel(char c){
return (c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
}
int maxVowels(string s, int k) {
int ans=0,sum=0,start=0;
char c;
for(int end=0;end<s.size();end++){
c=s[end];
if(isVowel(c)) sum++;
if(end>=k-1){
ans=max(sum,ans);
c=s[start++];
if(isVowel(c)) sum--;
}
}
return ans;
}
};

// TLE
// class Solution {
//     public:
//     int maxVowels(string s, int k) {
//         int cnt=0,ans=0,sz=s.size();
//         int i=0,j=k;
//         while(i<sz){
//             int tmp=i;
//             while(tmp<j){
//                 if(j>sz) j=sz;
//                 char c=s[tmp];
//                 if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
//                     cnt++;
//                 }
//                 tmp++;
//             }
//             ans=max(ans,cnt);
//             if(ans>k) return k;
//             cnt=0;
//             i++;
//             j++;
//         }
//         return ans;
//     }
// };


/*
LeetCode - Pseudo-Palindromic Paths in a Binary Tree

Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

Example 1:

Input: root = [2,3,1,3,1,null,1]
Output: 2
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 2:

Input: root = [2,1,1,1,3,null,null,null,null,null,1]
Output: 1
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 3:

Input: root = 
Output: 1

Constraints:

The given binary tree will have between 1 and 10^5 nodes.
Node values are digits from 1 to 9.
*/

/**
* 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:
int pseudoPalindromicPaths (TreeNode* root) {
vector<int> v(10,0);
dfs(root,v);
return ans;
}
private:
int ans=0;
void dfs(TreeNode* root, vector<int> v){
// dfs approach
// count each digit on the path to each leaf node
// a palindrome should either have
// - all digits present of EVEN count
// - or one digit of ODD count and the rest of them is even
// hence, check if there is only 0 or 1 odd numbers in the number of digits
if(!root) return;
v[root->val]++;
if(!root->left&&!root->right){
int odd=0;
for(int x:v){
if(x%2) odd++;
}
if(odd<=1) ans++;
return;
}
dfs(root->left,v);
dfs(root->right,v);
}
};


/*
LeetCode - Max Dot Product of Two Subsequences

Given two arrays nums1 and nums2.

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

Example 1:

Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
Output: 18
Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
Their dot product is (2*3 + (-2)*(-6)) = 18.
Example 2:

Input: nums1 = [3,-2], nums2 = [2,-6,7]
Output: 21
Explanation: Take subsequence  from nums1 and subsequence  from nums2.
Their dot product is (3*7) = 21.
Example 3:

Input: nums1 = [-1,-1], nums2 = [1,1]
Output: -1
Explanation: Take subsequence [-1] from nums1 and subsequence  from nums2.
Their dot product is -1.

Constraints:

1 <= nums1.length, nums2.length <= 500
-1000 <= nums1[i], nums2[i] <= 1000
*/

class Solution {
public:
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
int m=nums1.size(),n=nums2.size();
vector<vector<int>> dp(m, vector<int>(n,0));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
// calculate the dot product
dp[i][j]=nums1[i]*nums2[j];
// select
if(i&&j) dp[i][j]+=max(dp[i-1][j-1],0);
// don't select i-th element in nums1
if(i) dp[i][j]=max(dp[i][j],dp[i-1][j]);
// don't select j-th element in nums2
if(j) dp[i][j]=max(dp[i][j],dp[i][j-1]);
}
}
return dp[m-1][n-1];
}
};


The source code is available in corresponding repo below. Star and watch for timely updates!

## wingkwong / atcoder

### 🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Daily Coding Challenge (58 Part Series)

### Discussion   