Leetcode Weekly Contest 190

1455. 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.

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.

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) {

        String[] s = sentence.split(" ");
        int len = searchWord.length();
        StringBuffer sw = new StringBuffer(searchWord);
        String temp;
        for (int i = 0; i < s.length; i++) {
            if (len > s[i].length())
                continue;
            else
                temp =  s[i].substring(0, len);
            
            if (temp.contentEquals(sw))
                return i + 1;
        }
        return -1;
    }
}

1456. 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.

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) {
        Set<Integer> set = new HashSet<Integer>();
        set.add('a' - 'a');
        set.add('e' - 'a');
        set.add('i' - 'a');
        set.add('o' - 'a');
        set.add('u' - 'a');
        
        //子串中的最大元音个数, 统计每个子串的元音数,记录上一个子串的元音数
        int maxv = 0, count = 0, count_prev = 0; 
        int[] slide = new int[k];  //记录长度为k的每个子串中哪个是元音,元音置为1,其余为0
        
        for (int j = 0; j <= k - 1; j++) {
            if (set.contains(s.charAt(j) - 'a')) {
                slide[j] = 1; //为元音
                count++;
            }
        }
        maxv = Math.max(maxv, count);
        if (maxv == k)  //元音数的大小不可能大于子串长度
            return maxv;
        
        for (int i = k; i < s.length(); i++) {
            count_prev = count;
            if (set.contains(s.charAt(i) - 'a')) { //每次移动一位,判断最新位是否是元音
                if (slide[i % k] != 1) {  //循环更新slide,不一致时再改变
                    slide[i % k] = 1;  
                    count++;
                }
            }
            else {
                if (slide[i % k] != 0) {
                    slide[i % k] = 0;
                    count--;
                }
            }
            
            if (count != count_prev) {
                maxv = Math.max(maxv, count);
                if (maxv == k)
                    return maxv;
            }
            
        }
        return maxv;
    }
}

1457. 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 = [9]
Output: 1

 

Constraints:

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

1.dfs + array

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int pseudoPalindromicPaths (TreeNode root) {
        int[] digits = new int[10];
        
        return dfs(root, digits);

    }
    
    public int dfs(TreeNode root, int[] digits) {
        
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            
            int odd = 0;
            digits[root.val]++;
            
            for (int i = 0; i < digits.length; i++) {
                if (digits[i] % 2 == 1) 
                    odd++;
            }
            
            digits[root.val]--;
            if (odd == 0 || odd == 1) {
                return 1;
            }
            else 
                return 0;
        }
        else {
            
            digits[root.val]++;
            int ans = dfs(root.left, digits) + dfs(root.right, digits);
            digits[root.val]--;
            return ans;
        }
    }
    
}

2.

public int pseudoPalindromicPaths (TreeNode root) {
        return dfs(root, 0);
    }

    private int dfs(TreeNode root, int count) {
        if (root == null) return 0;
        count ^= 1 << (root.val - 1);
        int res = dfs(root.left, count) + dfs(root.right, count);
        if (root.left == root.right && (count & (count - 1)) == 0) res++;
        return res;
    }

  

1458. 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 [3] from nums1 and subsequence [7] 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 [1] from nums2.
Their dot product is -1.

Constraints:

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

 

1. https://leetcode.com/problems/max-dot-product-of-two-subsequences/discuss/648420/JavaC%2B%2BPython-the-Longest-Common-Sequence
public int maxDotProduct(int[] A, int[] B) {
        int n = A.length, m = B.length, dp[][] = new int[n][m];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                dp[i][j] = A[i] * B[j];
                if (i > 0 && j > 0) dp[i][j] += Math.max(dp[i-1][j-1], 0);
                if (i > 0) dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
                if (j > 0) dp[i][j] = Math.max(dp[i][j], dp[i][j - 1]);
            }
        }
        return dp[n-1][m-1];
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值