String - 16: Find Substrings formed using Concatenation of all given words

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 33

  • @VishalYadav-gk1kg
    @VishalYadav-gk1kg Рік тому +2

    Nice explanation sir, Thank you !

  • @ashishkalra9438
    @ashishkalra9438 3 роки тому +4

    After your explanation i realised why in the problem words have same length.
    Thank you so much for the wonderful explanation.
    Best wishes.
    Keep it up
    :)

  • @msh104utube
    @msh104utube 3 роки тому +7

    Great explanation. I think you should also relate these problems to the design pattern that is used to help students identify which pattern to use for each problem. For example, this one is sliding window. This would help to simplify coding interview problems.

    • @CodingSimplified
      @CodingSimplified  3 роки тому

      Thanks for your nice feedback. Sure, I'll try to add in my upcoming videos.

    • @msh104utube
      @msh104utube 3 роки тому

      @@CodingSimplified Thanks. Your videos are great and a great help.

  • @josereysalviojr.1490
    @josereysalviojr.1490 2 роки тому +2

    Instead of 2 for-loops; backtracking can also work:
    public List findSubstring(String s, String[] words) {
    final Map freqMap = new HashMap();
    for (final String word : words) {
    freqMap.put(word, freqMap.getOrDefault(word, 0) + 1);
    }
    final int wordCount = freqMap.size();
    final int wordLen = words[0].length();
    final Map currFreq = new HashMap();
    final Map lastPos = new HashMap();
    final List result = new ArrayList();
    for (int i = 0; i < s.length() - wordLen + 1;) {
    final String sub = s.substring(i, i + wordLen);
    if (freqMap.containsKey(sub)) {
    currFreq.put(sub, currFreq.getOrDefault(sub, 0) + 1);
    lastPos.putIfAbsent(sub, i);
    final int maxFreq = freqMap.get(sub);
    final int freq = currFreq.get(sub);
    if (freq > maxFreq) {
    i = lastPos.get(sub) + 1;
    lastPos.clear();
    currFreq.clear();
    } else if (freqMap.equals(currFreq)) {
    final int minPos = Collections.min(lastPos.values());
    result.add(minPos);
    i = minPos + 1;
    lastPos.clear();
    currFreq.clear();
    } else {
    i += wordLen;
    }
    } else {
    if (!currFreq.isEmpty()) {
    i = Collections.min(lastPos.values()) + 1;
    } else {
    i++;
    }
    lastPos.clear();
    currFreq.clear();
    }
    }
    return result;
    }

  • @spencerkinny
    @spencerkinny 4 роки тому +5

    Well explained it made my understanding clear!

  • @darshansimha2166
    @darshansimha2166 3 роки тому +2

    Such an elegant explanation. Thank you for this.

  • @priyanshubharti5198
    @priyanshubharti5198 2 роки тому

    Awesome Explanation man...

  • @techykush7192
    @techykush7192 3 роки тому +3

    hard question but your explanation is really outstanding

  • @aditikhandelwal135
    @aditikhandelwal135 2 роки тому

    Really an amazing explanation.

  • @kishorekumar7584
    @kishorekumar7584 2 роки тому

    Thankyou for the explanation

  • @RakeshKumar-he6ek
    @RakeshKumar-he6ek 3 роки тому +1

    Explained awesome!
    Thank you sir

  • @mohithadiyal6083
    @mohithadiyal6083 2 роки тому

    Amazing explanation

  • @svm8128
    @svm8128 4 роки тому +2

    Great experience

  • @TruongHoang-du9if
    @TruongHoang-du9if 4 роки тому

    Time complexity O(n * m * length), please correct the description

    • @CodingSimplified
      @CodingSimplified  4 роки тому +1

      Thanks for notifying. Looks like I missed updating in Description. Updated now.

    • @jitendranagar8962
      @jitendranagar8962 2 роки тому

      Shouldn't be it getting TLE..
      n=3*10^4
      m=5000
      length=30
      O(4.5*10^9)

    • @The_Promised_Neverland...
      @The_Promised_Neverland... 2 роки тому

      @@jitendranagar8962 nAH, the reason being he's using early pruning, hence as soon as there is a mismatch, he breaks out. BUT YEAH. WORST CASE CAN TLE

  • @himanshudagar8394
    @himanshudagar8394 3 роки тому +1

    Awesome :)

  • @codingSpeedWithGK
    @codingSpeedWithGK 3 роки тому

    what is the difference bwteen str==null and str.length()==0?

    • @tanmaydhal258
      @tanmaydhal258 2 роки тому +1

      Empty string is not null. String with length 0 is “”

  • @sneddengonsalves9320
    @sneddengonsalves9320 4 роки тому +1

    great explaination, but I hope I don't get this in my interview

    • @CodingSimplified
      @CodingSimplified  4 роки тому

      Yeah, I can understand it. Tough question it is :) BTW thanks for your nice feedback.

    • @cold1725
      @cold1725 3 роки тому

      If they ask this in interview, try this .
      class Solution {
      public List findSubstring(String s, String[] words) {


      HashMap given = new HashMap();
      HashMap formed = new HashMap();

      for(String word : words)
      given.put(word,given.getOrDefault(word,0)+1);


      List result = new ArrayList();

      int wordLength = words[0].length();


      for(int end=0;end

  • @shalmaleeshenolikar1045
    @shalmaleeshenolikar1045 Рік тому

    You lost me at concanet