Number of Substrings Containing All Three Characters - Leetcode 1358 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
11:15 - Coding Explanation

leetcode 1358

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

This felt so easy especially after yesterday's problem

donghyunsuh
Автор

7:38 Neecode speaks facts !! Best teacher man !! 😆😆

shashankjoshi
Автор

7:38 thank you neetcode! i really don't understand why I was dwelling on the video at that point. clicked off the video and coded up the solution and it passes all testcases. i really have to start learning more actively.

_PranavDesai
Автор

I felt violated and called out when neetcode asked me why am I still watching the video. Lol shiit I'm fcked lol

thespicycabbage
Автор

this should've been put up before yesterday's problem

mewtwosenpaii
Автор

thnx to your yesterday's explanation I was able to come up with a solution within a couple of minutes :))

exuply
Автор

I have been following your vedio's from a year or more and I really love your attitude, the way you teach in a very simple, practical and clear way. I have shared your channel to many of my friends as it will definitely add value to their preparation. Thankyou for all the great work/effort that you gave been doing. Keep up the same great work.
Coming to the solution to this problem. I have done it this way.
def numberOfSubstrings(self, s: str) -> int:
l=r= 0
count = 0
while r<len(s):
substr=s[l:r+1]
if len(substr) >= 3:
if 'a' in substr and 'b' in substr and 'c' in substr:
count+=(len(s)-r)
l+=1
else:
r+=1
else:
r+=1
return count

AdityaR-cb
Автор

It reminds me of a problem where I needed to find the number of substrings where all characters are vowels and k consecutive consonants.

yaroslavyatsyk
Автор

easy peazy (after yesterday's problem)

yhbarve
Автор

I was wondering how you'd do the substrings and the fact that we could just count the characters remaining hit me like a ton of bricks. Thank you for the superb guidance.

araneuskyuro
Автор

Bro, where are you??? We need your help in 3356. Zero Array Transformation II.

bhilaibiryanihouse-jm
Автор

Why didn't this one come before yesterday's... Also this is medium and yesterday's is medium...

Reck
Автор

actually we can use dictionary this way: def numberOfSubstrings(self, s: str) -> int:
left = 0
res = 0
n = len(s)
ctn = {'a': 0, 'b': 0, 'c': 0}
for right in range(n):
ctn[s[right]] += 1

while ctn['a'] >= 1 and ctn['b'] >= 1 and ctn['c'] >= 1:
res += (n - right)
ctn[s[left]] -= 1
left += 1
return res

SandeepWithAI
Автор

I enjoy watching your video explanations, even if I have solved the problem already. I like the way you explain things, and sometimes you will highlight a wrinkle in the problem that I've missed.

johnwarren
Автор

Subarrays with K Different Integers

key to yesterday's and today's

yashwantmoharil
Автор

7:40 I live in a free country that's why

knkootbaoat
Автор

it's actually nice learning new techniques by just doing the leetcode daily

gabrielignat
Автор

I have a doubt the brute force approach would take a time complexity of o(n^3) RIGHT???

GOKULAKRISHNANSARAVANAN-kj
Автор

could somebody explain me this solution?

class Solution:
def numberOfSubstrings(self, s: str) -> int:
res = i = 0 # maintain the left window pointer
d = {c:0 for c in 'abc'}
for j in range(len(s)):
d[s[j]] += 1
while all(d.values()):
d[s[i]] -= 1 # remove left char from window
i += 1
res += i # the number of substrings will be i
return res

slavikdoter
Автор

Any thoughts of why this might be bad code? (I know if we aren't limited to just a, b and c it could be less efficient but might be easier to understand and shorter to type than given solution... IDK any thoughts are appricited.):
class Solution:
def numberOfSubstrings(self, s: str) -> int:
hash = {"a": -1, "b": -1, "c": -1}
res = 0
for n in range(len(s)):
hash[s[n]] = n
res += min(hash.values()) + 1

return res

karlocehulic
visit shbcf.ru