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

ความคิดเห็น • 18

  • @sincelife5141
    @sincelife5141 4 ปีที่แล้ว +3

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

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      Thanks. Sure, these all problems are for sde1, sde2.....For more problems, please see various playlists in Channel.

  • @anand8412
    @anand8412 3 ปีที่แล้ว +1

    thanks for taking time to explain clearly.

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for your nice feedback. Keep watching.

  • @krishsharma2180
    @krishsharma2180 ปีที่แล้ว

    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

  • @shradhapaygude740
    @shradhapaygude740 4 ปีที่แล้ว +1

    Clean explanation..

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      Thanks Shradha for your feedback. Keep Watching.

  • @surajshaw2344
    @surajshaw2344 2 ปีที่แล้ว

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

  • @souradeepmajumdar4167
    @souradeepmajumdar4167 2 ปีที่แล้ว +1

    you are awsome!!!!!

    • @CodingSimplified
      @CodingSimplified  2 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

  • @reshmah1497
    @reshmah1497 3 ปีที่แล้ว +2

    Is this technique also known as sliding window technique?

  • @abilashreddy7828
    @abilashreddy7828 4 ปีที่แล้ว

    Thanks for this video .

  • @AyushKumar-vh7pg
    @AyushKumar-vh7pg 4 ปีที่แล้ว +1

    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

  • @rahulsinghai3033
    @rahulsinghai3033 4 ปีที่แล้ว +1

    Please create videos on Trie and it's applications

  • @pranavyeleti3499
    @pranavyeleti3499 3 ปีที่แล้ว

    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