Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3786
Abstract
w element to it while expanding the window and while shrinking the window, we can reduce the frequency of the character going out and if its frequency becomes zero we can remove the character from the hash table.</p><p id="6862">Whenever the window has at most K distinct characters, we can check if its size is the greatest we have seen and remember it.</p><p id="4d2e">If you are preparing for an interview, you might like these two articles:</p><div id="bf46" class="link-block">
<a href="https://ganeshpr227.medium.com/30-days-interview-preparation-plan-200-best-coding-questions-and-behavioural-interviews-3f8fc19c2361">
<div>
<div>
<h2>30 Days Interview Preparation Plan: 200+ best coding questions and Behavioural interviews</h2>
<div><h3>I have collected these questions from several great sources like geeksforgeeks, leetcode, interview experiences …</h3></div>
<div><p>ganeshpr227.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Oo5tZzjboKfxQVAbnOFSXg.png)"></div>
</div>
</div>
</a>
</div><div id="0763" class="link-block">
<a href="https://ganeshpr227.medium.com/key-concepts-and-terms-of-system-design-interview-notes-9bf882cf3730">
<div>
<div>
<h2>System Design — Key Concepts and Terms [Interview Notes] 🔥</h2>
<div><h3>This is a list of 20+ key terms and concepts in System Design, along with brief explanations for each.</h3></div>
<div><p>ganeshpr227.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*ZIVC5DhwYoAY8qg_y3QNyQ.jpeg)"></div>
</div>
</div>
</a>
</div><h2 id="cbf4">So the algorithm for Longest Substring with K Distinct characters can be:</h2><ul><li>Create a hash table to store a mapping between characters and their frequency.</li><li>Insert new characters from the string until we have K distinct characters in the window. Remember the window size as the answer (longest we have seen so far).</li><li>Then start expanding the window from the right or, in other words sliding the window by adding new elements.</li><li>If at any time the number of distinct characters in the window reaches more than K, we will shrink the window from the right side and reduce the frequency of the character moving out until we have only K distinct characters.</li><li>Whenever the character frequency in the hash table reaches 0, we will remove the character.</li><li>After every expansion and shrinking, we will check if the current window has the largest size we have seen and if so, we will remember it.</li></ul><p id="1bfd">An example will make things more clear, assume K = 2.</p><figure id="7a97"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LbX68hSsSfHtanVjx29MtA.png"><figcaption>Figure 1. Start with an empty window.</figcaption></figure><figure id="c3c1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zXLFnHP5qD5umZ52lWCmUA.png"><figcaption>Figure 2. Add the first character ‘a’ to the window.</figcaption></figure><figure id="7a21"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*iZFz-VzPl2rSqbe0hQLzwg.png"><figcaption>Figure 3. Add ‘b’ to the window.</figcaption></figure><figure id="c062"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NQMWBZNv0uzbdZgsv2HBug.png"><figcaption>Figure 4. Add ‘a’ to the window.</figcaption></figure><figure id="6bea"><img src="https://cdn-images-1.readmedium.com/v2/
Options
resize:fit:800/1*MsYsEQ0k8e9FaNxyGZBoVw.png"><figcaption>Figure 5. Add b to the window.</figcaption></figure><figure id="8b71"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-CV87O3J9AMMiH7MllBnbw.png"><figcaption>Figure 6. Add ‘c’ to the window.</figcaption></figure><figure id="c406"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OsLhVKWusmNlnA0WuuYstA.png"><figcaption>Figure 7. Shrink from left side.</figcaption></figure><figure id="0105"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NgwxD9ntBVJbqNwqEbgj2w.png"><figcaption>Figure 8. Shrink from left side.</figcaption></figure><figure id="a122"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*f2gs8Qbjwywcv_cCbbyd0w.png"><figcaption>Figure 9. Shrink from left side</figcaption></figure><figure id="0544"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*R-GS8cLtrYLJqXmibxgfeg.png"><figcaption>Figure 10. Add ‘b’ to the window.</figcaption></figure><figure id="fa1b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DFJI1AF3rF8fl47Rl0st8Q.png"><figcaption>Figure 11. Add ‘c’ to the window</figcaption></figure><figure id="83af"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ED0KG7FSxwM-fRLV9mMy6Q.png"><figcaption>Figure 12. Add ‘c’ to the winodw.</figcaption></figure><figure id="9fc1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*g28VsV8vU4WDRojUk3grnA.png"><figcaption>Figure 13. Add ‘a’ to the window.</figcaption></figure><p id="a2e9">The following code is in python with proper comments. And, If you want c++, <a href="https://ganeshpr227.medium.com/just-c-code-for-all-the-problems-i-have-practiced-58bfeeedd006#33ba">read here</a>.</p><h1 id="253b">Code (Python)</h1>
<figure id="b3fe">
<div>
<div>
<iframe class="gist-iframe" src="/gist/PrasadGanesh/dc5fd3506edccbbef88fa5bccdbb2aca.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><div id="d937"><pre><span class="hljs-symbol">Output:</span>
<span class="hljs-number">5</span></pre></div><h1 id="e3fa">Code (CPP)</h1>
<figure id="28f0">
<div>
<div>
<iframe class="gist-iframe" src="/gist/PrasadGanesh/e866ef6b018ac9fd5a3cf18cfb48a49e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="ebf2">Time & Space Complexity</h1><p id="a748"><b>Time</b>: The outer for loop runs for n times, where n is the number of characters in the string and the inner while loop runs for each character only once hence the time complexity is linear or O(n).</p><p id="c916"><b>Space</b>: We are using a dictionary, which at any given time can have at most (K+1) keys; hence the space complexity is proportional to K, <b>O(K)</b>.</p><p id="719b">Thank you for reading!</p><p id="eda4"><i>P.S.: If you like this uninterrupted reading experience on this beautiful platform of Medium.com, consider supporting the writers of this community by signing up for a membership <a href="https://ganeshpr227.medium.com/membership">HERE</a>. It only costs $5 per month and helps all the writers.</i></p><p id="d17a"><i>If you liked what you just read, a clap would be highly appreciated. You can be generous in clapping; it shows me how much you enjoyed this story. And if you didn’t like it? Please do comment</i>😋<i>!</i></p><p id="72ca">You can also follow me <a href="https://ganeshpr227.medium.com/">HERE</a> to get updates on the stories that I write.</p></article></body>