String - 9: Find longest sub-string length with K distinct characters | Java Implementation

preview_player
Показать описание
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

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Explained beautifully and we expect more videos prep on sde1!!

sincelife
Автор

thanks for taking time to explain clearly.

anand
Автор

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

AyushKumar-vhpg
Автор

Is this technique also known as sliding window technique?

reshmah
Автор

Please create videos on Trie and it's applications

rahulsinghai
Автор

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

krishsharma
Автор

here only the maxLength have been found, how to find the string with k distinct characters?

surajshaw
Автор

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

pranavyeleti