Nice video! Here is the solution for vowels # max number of vowels in a string of size k def max_vowels(s, k): vowels = ("a", "e", "i", "o", "u") maxvowels = 0 for ch in s[:k]: if ch in vowels: maxvowels += 1 total = maxvowels for i in range(len(s) - k): if s[i] in vowels: total -= 1 if s[i + k] in vowels: total += 1 maxvowels = max(maxvowels, total) return maxvowels
Thank you. I've been struggling to wrap my mind around ChatGPT's explanation of sliding window technique for a few days now, this is so much simpler. def count_vowels_efficientcode(word:str, k:int) -> int: VOWELS = set('aeiou') max_vowels:int = 0 n:int = len(word) if n
nicely done. the best tutorial ever. please continue making new vide like this, here is my solution: is it correct? def mnv(s,k): v = "aeoiu" ms =0 for i in range(k): if s[i] in v: ms +=1 max_substring = ms for i in range(len(s) - k): if s[i] in v: ms -= 1 if s[i+k] in v: ms += 1 max_substring = max(max_substring, ms) return max_substring how would you solve it?????
great video. Had fun brushing up on this algorithm. function maxNumOfVowels(s, k) { let maxNum = 0; function isVowel(c) { return /[aeiou]/.test(c) ? 1 : 0; } let leftBound = 0; let rightBound = k-1; let firstSlice = s.substring(leftBound, k); for (let index = 0; index < firstSlice.length; index++) { const element = firstSlice[index]; maxNum += isVowel(element); } let currentCount = maxNum; while (rightBound < s.length - 1) { leftBound++; rightBound++; if(isVowel(s[leftBound-1])) { currentCount -= 1; } if (isVowel(s[rightBound])) { currentCount += 1; } maxNum = Math.max(currentCount, maxNum); } return maxNum; }
When you first introduced the problem, we wanted to pick out the best set of five books, not what the price of the best set was. That means we were looking for argmax rather than max. so the code would be closer to: def best_set_for_price(items, k): if len(items) max): max = offset argmax = i + 1 return argmax As a bonus, note how we never need to calculate the initial sum.
Great video!!. This technique can also be leveraged in rolling hash calculations. Solution to the sliding window to calculate max vowels count in a string class Solution: def maxVowels(self, s: str, k: int) -> int: if len(s) < k: return 0 total = 0 lookUpv = {'a':True, 'e':True, 'i':True, 'o':True, 'u':True} for c in s[:k]: if c in lookUpv: total += 1 maxTotal = total for i in range(len(s)-k): char_to_add = s[i+k] if char_to_add in lookUpv: total += 1 char_to_remove = s[i] if char_to_remove in lookUpv: total -= 1 maxTotal = max(maxTotal,total) return maxTotal
tried this in JS. Used some helper functions so I don't dilute the main function: // HELPER FUNCTIONS function isVowel(str) { let vowels = "aeiou"; return vowels.includes(str); } function countVowels(str) { let count = 0; for (let i = 0; i < str.length; i++) { if (isVowel(str[i])) count += 1; } return count; } // MAIN FUNCTION function maxVowels(s, k) { let total = countVowels(s.slice(0, 5)); let maxVowels = total; for (let i = 0; i < s.length - k; i++) { if (isVowel(s[i])) total -= 1; if (isVowel(s[i + k])) total += 1; if (maxVowels < total) maxVowels = total; } return maxVowels; }
i dont understand why the brute force for best_total_price has the "for i in range(len(prices)-k+1)" instead of "for i in range(len(prices)-k)". Could anyone explain this?
I figured out why. len(prices)-k+1 is the starting index of the last combination of the books. The calculations of brute force and sliding window have different index ranges.
I have come up with a solution but it seems like brute force, and if so, someone help me optimize it: static int maxVowelsNum(String s, int k) { var list = List.of('a', 'e', 'i', 'o', 'u'); var p1 = 0; var p2 = k + 1; var max = 0; var count = 0; while (p2 < s.length()) { var chars = s.substring(p1, p2); for (var ch : chars.toCharArray()) { if (list.contains(ch)) { count++; } } max = Math.max(max, count); p1++; p2++; count = 0; } return max; } Thank you.
Great explanation! Someone get this man an award!
Your videos are impressive! Can you do all of the major technical interview patterns and the intuition behind them? Thanks 💯
Thanks a lot!
Nice video! Here is the solution for vowels
# max number of vowels in a string of size k
def max_vowels(s, k):
vowels = ("a", "e", "i", "o", "u")
maxvowels = 0
for ch in s[:k]:
if ch in vowels:
maxvowels += 1
total = maxvowels
for i in range(len(s) - k):
if s[i] in vowels:
total -= 1
if s[i + k] in vowels:
total += 1
maxvowels = max(maxvowels, total)
return maxvowels
perfect! good job
Would you say in this optimal way to be O(n) then because of how the two if statements are linear and all right?
Beautiful explanation! Thank you! Was struggling with this for quite some time.
thanks man, you explained the thing really well. I had fun taking the class
Thank you. I've been struggling to wrap my mind around ChatGPT's explanation of sliding window technique for a few days now, this is so much simpler.
def count_vowels_efficientcode(word:str, k:int) -> int:
VOWELS = set('aeiou')
max_vowels:int = 0
n:int = len(word)
if n
Amazing videos, content is explained so well with impressive animations!
Thank you!
nicely done. the best tutorial ever. please continue making new vide like this,
here is my solution: is it correct?
def mnv(s,k):
v = "aeoiu"
ms =0
for i in range(k):
if s[i] in v:
ms +=1
max_substring = ms
for i in range(len(s) - k):
if s[i] in v:
ms -= 1
if s[i+k] in v:
ms += 1
max_substring = max(max_substring, ms)
return max_substring
how would you solve it?????
Best video on this topic all over youtube. 😍
Please continue making videos. I love them ❤️
Sure! thanks a lot
Plz make more videos no one can beat your level Absolutely Brilliant
Thanks!
The name should be caterpillar 🙂
Beautiful visualisation! and explanation of time complexities and algorithm, love the video so much!!❤
Great explanation! Awesome visualisation, it is easy to understand the problem and the solution.
great video. Had fun brushing up on this algorithm.
function maxNumOfVowels(s, k) {
let maxNum = 0;
function isVowel(c) {
return /[aeiou]/.test(c) ? 1 : 0;
}
let leftBound = 0;
let rightBound = k-1;
let firstSlice = s.substring(leftBound, k);
for (let index = 0; index < firstSlice.length; index++) {
const element = firstSlice[index];
maxNum += isVowel(element);
}
let currentCount = maxNum;
while (rightBound < s.length - 1) {
leftBound++;
rightBound++;
if(isVowel(s[leftBound-1])) {
currentCount -= 1;
}
if (isVowel(s[rightBound])) {
currentCount += 1;
}
maxNum = Math.max(currentCount, maxNum);
}
return maxNum;
}
this was the best explanation I have seen!
THANK YOUU!
When you first introduced the problem, we wanted to pick out the best set of five books, not what the price of the best set was. That means we were looking for argmax rather than max. so the code would be closer to:
def best_set_for_price(items, k):
if len(items) max):
max = offset
argmax = i + 1
return argmax
As a bonus, note how we never need to calculate the initial sum.
Thanks for your interesting answer
mind == blown
Great channel. Great illustrations and examples....🙋♂️
Absolute genius, please continue making videoss
Thanks! Sure
You know you are gonna learn something when the English speaker has an foreign accent ty for the tuto dude you insane
keep uploading this kind of videos. your videos are awesome!!!
Sure! Thanks a lot
Best explanation ever!
Your lectures are good 😊.keep posting vedios
Great video!!. This technique can also be leveraged in rolling hash calculations.
Solution to the sliding window to calculate max vowels count in a string
class Solution:
def maxVowels(self, s: str, k: int) -> int:
if len(s) < k:
return 0
total = 0
lookUpv = {'a':True, 'e':True, 'i':True, 'o':True, 'u':True}
for c in s[:k]:
if c in lookUpv:
total += 1
maxTotal = total
for i in range(len(s)-k):
char_to_add = s[i+k]
if char_to_add in lookUpv:
total += 1
char_to_remove = s[i]
if char_to_remove in lookUpv:
total -= 1
maxTotal = max(maxTotal,total)
return maxTotal
Yes, I'm planning a video on Rabin-Karp algorithm, which uses rolling hash
Hey, the video on Rabin-Karp is out
@@insidecode Great!!.Thanks
please make more videos like this on recursion medium hard questions
Great explanation!
I love these kind of explanations
Made things very clear, thank you so much
This is the best explanation I have ever watched. Thank you!
here is a node solution for the exercise at the end of the video:
import { strictEqual } from "assert";
const vowels = new Map([
["a", "a"],
["e", "e"],
["i", "i"],
["o", "o"],
["u", "u"],
]);
const isVowel = (v: string) => vowels.has(v);
const countVowels = (string: string, span = 5) => {
let vowelsCount = 0;
let maxVowels = 0;
for (let i = 0; i < string.length; i++) {
if (i - span - 1 > -1) {
if (isVowel(string[i - span - 1])) {
vowelsCount--;
}
}
if (isVowel(string[i])) {
vowelsCount++;
}
if (vowelsCount > maxVowels) {
maxVowels = vowelsCount;
}
}
return maxVowels;
};
const input = "bacacbefaobeacfe";
strictEqual(
countVowels(input),
4,
"The expected ammount of vowels was 4, got " + countVowels(input)
);
Great explanation and illustration
Sounds like the key word to use the Sliding Window is "contiguous" when dealing with an Array?
Your video help me a lot! Thank you
Impressive Explanation ❤️
thankyou sir, great explanation☺
Well done, thank you for sharing!
tried this in JS. Used some helper functions so I don't dilute the main function:
// HELPER FUNCTIONS
function isVowel(str) {
let vowels = "aeiou";
return vowels.includes(str);
}
function countVowels(str) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (isVowel(str[i])) count += 1;
}
return count;
}
// MAIN FUNCTION
function maxVowels(s, k) {
let total = countVowels(s.slice(0, 5));
let maxVowels = total;
for (let i = 0; i < s.length - k; i++) {
if (isVowel(s[i])) total -= 1;
if (isVowel(s[i + k])) total += 1;
if (maxVowels < total) maxVowels = total;
}
return maxVowels;
}
Crazy explanation 🔥😎
i dont understand why the brute force for best_total_price has the "for i in range(len(prices)-k+1)" instead of "for i in range(len(prices)-k)". Could anyone explain this?
because the last element should be included
I figured out why. len(prices)-k+1 is the starting index of the last combination of the books. The calculations of brute force and sliding window have different index ranges.
Nice content, dropping a like right now
Very good explanation woow
Nice man 👍
Thank u bro ♥️
Thanks a lot.
very helpful, thanks!
Thanks
Priceless video
awesome video!
thaankyou it is so clear
How do you make these animations?
I use PowerPoint
I like your videos because your content with animation and your hard work, love you dude❤️. But I am not gay.
awesome channel
great video! what software did you use to create this video?
Thanks! I used PowerPoint
Just awesome!!!!
this channel is so top
you are genius
To the point!
I have come up with a solution but it seems like brute force, and if so, someone help me optimize it:
static int maxVowelsNum(String s, int k) {
var list = List.of('a', 'e', 'i', 'o', 'u');
var p1 = 0;
var p2 = k + 1;
var max = 0;
var count = 0;
while (p2 < s.length()) {
var chars = s.substring(p1, p2);
for (var ch : chars.toCharArray()) {
if (list.contains(ch)) {
count++;
}
}
max = Math.max(max, count);
p1++;
p2++;
count = 0;
}
return max;
}
Thank you.
"the size doesnt have an impact"
5:03 I have no clue what you just said here
what accent is this?
thanks