String - 9: Find longest sub-string length with K distinct characters | Java Implementation
ฝัง
- เผยแพร่เมื่อ 5 ก.พ. 2025
- Source Code:thecodingsimpl...
Solution:
We take a Map, which'll give me info about unique characters present, Map key will be character & value will be the occurrence of that character
We'll take a max_length variable, where we'll store the solution
we'll take two variable start & end, which'll point to 1st character in string initially
Now we iterate the string & keep on putting character in map. If size of value is greater than K, then we decrease the character from start index. If occurrence becomes 0, we remove from map.
max_length will be updated by end - start + 1
At last we return, max_length as answer
Time Complexity: O(n)
Space Complexity: O(n)
Do Watch video for more info
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my TH-cam channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.
★☆★ SUBSCRIBE TO ME ON TH-cam: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
Explained beautifully and we expect more videos prep on sde1!!
Thanks. Sure, these all problems are for sde1, sde2.....For more problems, please see various playlists in Channel.
thanks for taking time to explain clearly.
Thanks for your nice feedback. Keep watching.
in second while loop ,where map size > k, can't we directly remove the first element. why we first decrementing the element then removing the element
Clean explanation..
Thanks Shradha for your feedback. Keep Watching.
here only the maxLength have been found ,how to find the string with k distinct characters?
you are awsome!!!!!
Thanks for your nice feedback. Keep Watching.
Is this technique also known as sliding window technique?
That's right.
Thanks for this video .
Good work sir
Can you make more videos on various questions of graph till now you have only taught BFS and DFS and few concepts we want you to teach some questions related to graph
Please make videos on them
Sure, I'll try to add soon.
Please create videos on Trie and it's applications
Sure, will try to do it. Thanks.
if string is aaaabbbb and k=3 then it should not return maxium length but ur algo will return 8 as max length?.can u please tell me