Find the Smallest Window that Contains all Characters of String itself | GFG | Amazon 🔥

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

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

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

    Thank you for a wonderful explanation. I tried implementing with python
    def shortestSubstring(s):
    n = len(s)
    k = len(set(s))
    start = 0
    end = 0
    count = 0
    hashmap = dict()
    ans_str = ""
    min_len = 10**9 + 7

    while end < n:
    curr_ch = s[end]
    if curr_ch in hashmap:
    hashmap[curr_ch] += 1

    else:
    hashmap[curr_ch] = 1
    count += 1

    if count == k:
    if min_len > (end - start + 1):
    min_len = end - start + 1
    ans_str = s[start: end+1]

    while count == k:
    if min_len > (end - start + 1):
    min_len = (end - start + 1)
    ans_str = s[start: end+1]

    hashmap[s[start]] -= 1

    if hashmap[s[start]] == 0:
    count -= 1
    del hashmap[s[start]]

    start += 1

    end += 1

    #print(min_len)
    return ans_str
    t = int(input())
    while t > 0:
    s = input()
    print(shortestSubstring(s))
    t = t-1

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

    It won't work when the given pattern doesn't have all distinct element of the given string, or when repeated element are present in the pattern.

  • @SajanKumar-ec2us
    @SajanKumar-ec2us ปีที่แล้ว +1

    How time complexity ooder of n here j goes till last index and due to srink i also can go last index .

  • @SajanKumar-ec2us
    @SajanKumar-ec2us ปีที่แล้ว +2

    Run time error comes

  • @Sohailkhan-re8sn
    @Sohailkhan-re8sn 2 ปีที่แล้ว +3

    It is giving TLE on GFG.

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

    Time limit exceeded error in gfg website
    Can you provide more optimized solution of this problem?

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

      is it still showing tle??

    • @romilpatel6227
      @romilpatel6227 ปีที่แล้ว +5

      it is cause you are using set and uski time complexity is nlogn
      so use unordered set which usually performs operations in O(1)

    • @shubhamsahu2715
      @shubhamsahu2715 10 หลายเดือนก่อน

      thanks bro was facing the same problem n it worked....@@romilpatel6227

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

    Java implementation for the updated question:
    public static String smallestWindow(String s, String p){
    int n=s.length();
    int m=p.length();
    if(m>n){
    return "-1";
    }
    if(m==n){
    return s.equals(p)?p:"-1";
    }
    HashMap pm=new HashMap();
    for(int i=0;i0){
    ctz--;
    }
    i++;
    }
    }
    j++;
    }
    if(st==-1){
    return "-1";
    }
    return s.substring(st,e+1);
    }

  • @yn-ey9sw
    @yn-ey9sw 3 ปีที่แล้ว +11

    bhaiya ip address vaala karva do if possible, it is at index 87 in the excel sheet

    • @VishalKumar-sm8bo
      @VishalKumar-sm8bo 3 ปีที่แล้ว +1

      +1

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

      code is simpe as ip address must be of length 12 so we use loop of 3,4,4 it will give right ans
      #include
      using namespace std;
      vectorvalid(string s)
      {
      vector ans;
      if(s.length()12)
      return ans;
      for(int i=0;i1)
      continue;
      if(d[0]=='0'&&d.length()>1)
      continue;
      if(stoi(a)

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

      @@devratnasawaitul8844 Do u know how to do this question with backtracking instead of nested for loops???

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

    how can someone learn each and every questions like him, no valid explanation just learnt the solutions

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

    tle

  • @journey_of_me_vs_me
    @journey_of_me_vs_me 4 หลายเดือนก่อน

    Verrryyyy Good Solution :))

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

    you remove all a , you should not remove all charactor of a . suppose a does not come at the end then your code could be wrong.

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

    Why you are decrementing when the count is 1 it may be more than 1 also right?

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

    Solution is Not covered all Corner Cases.......SHOULD IMPROVED FOR UPDATED QUE

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

    Why u are just decrement count if m[s[i]]==1 , as if it is greater then also we have to decrement count right?

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

      If it was greater than 1 then count of distinct characters won't change , but if it is 1 after decrement it becomes 0 which means now we dont have all the unique cahracters

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

    Nicely explained

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

    // For all those who did not get it (implementation)
    class Solution{
    public:
    const int no_of_chars = 256;
    // Function to find smallest window containing
    // all characters of 'pat'
    string findSubString(string str) {

    int len1 = str.length();
    string pat = "";

    set s;
    for (int i = 0; i < len1; i++)
    s.insert(str[i]);
    for (auto i = s.begin(); i != s.end(); i++)
    pat += (*i);

    int len2 = pat.length();

    // check if string's length is less than pattern's
    // length. If yes then no such window can exist
    if (len1 < len2) {
    return "";
    }

    int hash_pat[no_of_chars] = {0};
    int hash_str[no_of_chars] = {0};

    // store occurrence ofs characters of pattern
    for (int i = 0; i < len2; i++) hash_pat[pat[i]]++;

    int start = 0, start_index = -1, min_len = INT_MAX;

    // start traversing the string
    int count = 0; // count of characters
    for (int j = 0; j < len1; j++) {
    // count occurrence of characters of string
    hash_str[str[j]]++;

    // If string's char matches with pattern's char
    // then increment count
    if (hash_pat[str[j]] != 0 && hash_str[str[j]] hash_pat[str[start]] ||
    hash_pat[str[start]] == 0) {

    if (hash_str[str[start]] > hash_pat[str[start]])
    hash_str[str[start]]--;
    start++;
    }

    // update window size
    int len_window = j - start + 1;
    if (min_len > len_window) {
    min_len = len_window;
    start_index = start;
    }
    }
    }

    // If no window found
    if (start_index == -1) {
    return "";
    }

    // Return substring starting from start_index
    // and length min_len
    return str.substr(start_index, min_len);
    }
    };
    //Although his approach is great but the implementation is wrong

  • @Rahulkumar-pd4wv
    @Rahulkumar-pd4wv 2 ปีที่แล้ว +2

    It is giving TLE on GFG can we optimize it more .

    • @HarshKumar-pn1ri
      @HarshKumar-pn1ri 2 ปีที่แล้ว

      Make sure you are using unordered map.

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

    thanks for your awesome explanation 🔥🔥

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

    Working Solution in Java for Smallest distinct window gfg:
    class Solution {
    public String findSubString(String str) {
    Map map = new HashMap();
    Set set = new HashSet();
    for (char c : str.toCharArray()) {
    set.add(c);
    }
    int i = 0, j = 1;
    int n = set.size();
    for (Character ch : set) {
    map.put(ch, 0);
    }
    map.put(str.charAt(i), 1);
    int c = 0;
    c++;
    int start = 0, end = 0;
    int mi = Integer.MAX_VALUE;
    while (i

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

    Confusing

  • @oneminuteanimation-uz5yb
    @oneminuteanimation-uz5yb ปีที่แล้ว

    what is tc and sc for this qn
    \

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

    awesome work

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

    bhai question kya pucha hai tum solve kya karr rahe ho.

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

    Bro please give the solution of the same program in c language 🙏

  • @SajanKumar-ec2us
    @SajanKumar-ec2us ปีที่แล้ว

    Not satisfied all test case of gfg

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

    Thanks :)

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

    thank you byaa

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

    Nice video bro.

  • @TapanKPatra
    @TapanKPatra 3 ปีที่แล้ว +7

    His accent is so irritating....

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

    getting tle......

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

    longest common subsequence bna do jaldi

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

      This is a dynamic programming question so we will make this questions video in dynamic programming section.

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

      th-cam.com/video/4Urd0a0BNng/w-d-xo.html
      follow this link to understand lcs problem

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

      Watch lcs by pepcoding, best explanation

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

    Why we are not incrementing the value of "a" bt we are incrementing the count of rest ones " b" , " c"?

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

      He forgot to do that , later he changed it.

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

    Brother : In the excel sheet its about finding all character not at-least 1 character :
    Kindly look into this

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

    Find the shortest substring in a given string that contains all the characters of a given word
    Tell me answer in today night

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

    string findSubString(string str)
    {
    // Your code goes here
    unordered_map mymap;
    set st;
    for(int i=0;i