Find longest substring with K unique characters - Sliding Window Algorithm

preview_player
Показать описание
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.

Example 1:

Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:

Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.

Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).

=========================================================

Other playlists in the channel that you can follow:

---------------------------------------------------------
Please click the LIKE button so that this kind of content can be seen by as many as possible aspiring engineers and professionals. Doing so gives me even more motivation to post such content in the future.
Please comment below and let me know if you have any questions or concerns.

Additionally, don't forget to click the bell symbol to receive notifications and subscribe to the channel.
Follow us on:
----------------------------------------------------------
Created and Instructed by:
Varsha Das
Mid-senior Software Engineer, Content Curator of @Code With Ease
Follow her on:

Code With Ease is meant to make problem-solving in programming easier. For DSA preparation, we post topic-specific videos pertaining to Coding Interview topics so that everyone may approach such questions with a clear thought process and achieve the best outcomes. In addition, we make an effort to include different "Primer Series" that teach particular concepts or algorithms. We want to be the one-stop solution for everything pertaining to DSA preparation and programming foundations in general, saving candidates from having to visit many sources in order to receive structured content.
Additionally, we believe that learning to code entails much more than only "cracking the coding interview." The long-term goal of this channel is to create more proficient problem-solvers in this field so that, in the end, we can create fantastic products and be able to resolve challenging issues in our environment to improve the quality of life for everyone.

#dsa #codewithease #datastructureandalgorithm #programming #javaprogramming #codinginterviewquestions #stringsinjava #slidingwindow #longestsubstring #kuniquecharacters

longest k unique characters substring

longest k unique characters substring
longest substring with at most k distinct characters
count with k different characters
longest k unique character substring
longest substring with at least k repeating characters
find longest substring with k unique character
find longest substring with k unique characters
longest substring with k distinct characters (medium)
longest substring with k distinct characters leetcode
longest substring with ‘k’ distinct characters
find the longest substring with k unique character
k unique characters
longest k interspace substring
find-longest-substring-containing-k-distinct-characters
find the longest substring with k unique characters
r find longest string in column
r find longest string in vector
r longest common substring
longest substring with at most k distinct characters leetcode
Рекомендации по теме
Комментарии
Автор

This is exactly what I was looking for. Amazing explanation
Please make a playlist which is based on different coding patterns so that we can identify the pattern of problems and we can attempt it.
Thank you

vipinthakur
Автор

Great explanation. Keep posting similar videos.

ramannaagre
Автор

super mam keep going for more number of coding questions🤩🤩🤩🤩

studentcare
Автор

in place of "While" we can just use a if statement for shrinking right? bcz there is no need of shrinking the window less than the already found longest sub array's length ..there by T.C reduces to O(N) from O(N)

manusklm
Автор

Great Explanation...
Can you explain that why you've not used set in this problem, as we want to store unique characters & their count ? What is the advantage of using unordered map over a set ?

priyanshkumar
Автор

Hey, could you help me understand why is there a while loop on line 35 and not an if statement? Thank you for the video, was very useful. :)

mecies
Автор

If we want to print the characters of that string how will modify the code

bharanitharan
Автор

String s = "abc.b.cc";
find the longest substring length without java ...ma'am @Code With Ease please provide the code

Alok_Roy
Автор

didi please please make a detailed vedio on variable size sliding window it would of great help

udaytewary