DMITRII’s Substack

Share this post

Leetcode daily # 5.05.2023

dmitriisamoilenko.substack.com

Discover more from DMITRII’s Substack

I post a problem that I solve every single day
Continue reading
Sign in

Leetcode daily # 5.05.2023

[1456. Maximum Number of Vowels in a Substring of Given Length]

DMITRII SAMOILENKO
May 5, 2023
Share this post

Leetcode daily # 5.05.2023

dmitriisamoilenko.substack.com
Share

5.05.2023

1456. Maximum Number of Vowels in a Substring of Given Length medium

fun maxVowels(s: String, k: Int): Int {
    val vowels = setOf('a', 'e', 'i', 'o', 'u')
    var count = 0
    var max = 0
    for (i in 0..s.lastIndex) {
        if (s[i] in vowels) count++
        if (i >= k && s[i - k] in vowels) count--
        if (count > max) max = count
    }
    return max
}

blog post

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/203

Intuition

Count vowels, increasing them on the right border and decreasing on the left of the sliding window.

Approach

  • we can use Set to check if it is a vowel

  • look at a[i - k] to detect if we must start move left border from i == k

Complexity

  • Time complexity:
    O(n)

  • Space complexity:
    O(1)

Thanks for reading DMITRII’s Substack! Subscribe for free to receive new posts and support my work.

Share this post

Leetcode daily # 5.05.2023

dmitriisamoilenko.substack.com
Share
Previous
Next
Comments
Top
New

No posts

Ready for more?

© 2023 DMITRII SAMOILENKO
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing