Wonderland
  • README.md
  • Notebook
    • Crypto
      • Solana
        • Troubleshooting
          • BPF SDK path does not exist
    • Language
      • Rust
        • Reference
          • Capturing the Environment with Closures
          • Understanding &mut &mut Reference
      • C++
        • Reference
          • Code for MS rand() and srand() in VC++ 6.0
  • Textbook
    • Serials
      • A Real-Time Cryptocurrency Ticker Dashboard
        • 0 - Some Preparation
    • Frontend
      • A Simple Progress Bar
      • A React Ribbon Component
      • An Easy to Use React DnD Sortable Component
      • Sticky Header, Sticky Footer and Fluid Content
      • How To Set Same Height As Width In CSS
  • dictionary
    • Alphabet
      • MySQL
      • FFmpeg
    • Algorithm
      • Diary
        • 2022
          • 07
            • 2022-07-02
            • 2022-07-01
          • 06
            • 2022-06-30
            • 2022-06-29
            • 2022-06-28
            • 2022-06-27
            • 2022-06-26
            • 2022-06-25
            • 2022-06-24
            • 2022-06-23
            • 2022-06-22
            • 2022-06-21
            • 2022-06-20
            • 2022-06-19
            • 2022-06-18
            • 2022-06-17
            • 2022-06-16
            • 2022-06-15
            • 2022-06-14
            • 2022-06-13
            • 2022-06-12
            • 2022-06-11
            • 2022-06-10
            • 2022-06-09
            • 2022-06-08
            • 2022-06-07
            • 2022-06-06
            • 2022-06-05
            • 2022-06-04
            • 2022-06-03
            • 2022-06-02
            • 2022-06-01
          • 05
            • 2022-05-31
            • 2022-05-30
            • 2022-05-29
            • 2022-05-28
            • 2022-05-27
            • 2022-05-26
            • 2022-05-25
            • 2022-05-24
            • 2022-05-23
            • 2022-05-22
            • 2022-05-21
            • 2022-05-20
            • 2022-05-19
            • 2022-05-18
            • 2022-05-17
            • 2022-05-16
            • 2022-05-15
    • Troubleshooting
      • A Weird Python Command Not Found Problem
Powered by GitBook
On this page
  • 438. Find All Anagrams in a String
  • Description
  • Solution
  • 713. Subarray Product Less Than K
  • Description
  • Solution
  • 209. Minimum Size Subarray Sum
  • Description
  • Solution
  1. dictionary
  2. Algorithm
  3. Diary
  4. 2022
  5. 06

2022-06-01

Last updated 2 years ago

Description

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

Constraints:

  • 1 <= s.length, p.length <= 3 * 104

  • s and p consist of lowercase English letters.

Solution

Approach #0

func findAnagrams(s string, p string) (ans []int) {
    target, current := make([]int, 26), make([]int, 26)
    count := 0
    for _, ch := range p {
        target[ch-'a']++
    }
    for _, t := range target {
        if t != 0 {
            count++
        }
    }
    i, j, sig := 0, 0, 0
    for j < len(s) {
        in := s[j] - 'a'
        j++
        if target[in] > 0 {
            current[in]++
            if target[in] == current[in] {
                sig++
            }
        }
        for j-i >= len(p) {
            if sig == count {
                ans = append(ans, i)
            }
            out := s[i] - 'a'
            i++
            if target[out] > 0 {
                if current[out] == target[out] {
                    sig--
                }
                current[out]--
            }
        }
    }
    return ans
}

Description

Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

Example 1:

Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.

Example 2:

Input: nums = [1,2,3], k = 0
Output: 0

Constraints:

  • 1 <= nums.length <= 3 * 10^4

  • 1 <= nums[i] <= 1000

  • 0 <= k <= 10^6

Solution

Approach #0

func numSubarrayProductLessThanK(nums []int, k int) (ans int) {
    cur := 1
    i, j := 0, 0
    for i < len(nums) {
        if j >= len(nums) {
            cur = 1
            i++
            j = i
            continue
        }
        in := nums[j]
        if cur*in < k {
            ans++
            cur = cur * in
            j++
        } else {
            cur = 1
            i++
            j = i
        }
    }
    return ans
}

Approach #1

func numSubarrayProductLessThanK(nums []int, k int) (ans int) {
    i, cur := 0, 1
    for j, num := range nums {
        cur *= num
        for ; i <= j && cur >= k; i++ {
            cur /= nums[i]
        }
        ans += j - i + 1
    }
    return
}

Description

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2:

Input: target = 4, nums = [1,4,4]
Output: 1

Example 3:

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0

Constraints:

  • 1 <= target <= 10^9

  • 1 <= nums.length <= 10^5

  • 1 <= nums[i] <= 10^5

Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).

Solution

Approach #0

func minSubArrayLen(target int, nums []int) (ans int) {
    ans = len(nums) + 1
    i, cur := 0, 0
    for j, num := range nums {
        cur += num
        for ; i <= j && cur >= target; i++ {
            if cur >= target {
                ans = min(ans, j-i+1)
            }
            cur -= nums[i]
        }
    }
    if ans == len(nums)+1 {
        ans = 0
    }
    return
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

438. Find All Anagrams in a String
713. Subarray Product Less Than K
209. Minimum Size Subarray Sum