String Matching in an Array - Leetcode 1408 - Python

Поделиться
HTML-код
  • Опубликовано: 9 янв 2025

Комментарии • 44

  • @LeonLeonLeonardo
    @LeonLeonLeonardo 3 дня назад +25

    Sometimes the brute force is the only way 😶‍🌫

  • @anonanon6596
    @anonanon6596 3 дня назад +3

    I guess this is a bug raport for your website:
    In the practice section, if you set a question as solved and then change list (for example from blind 75 to 150), the question gets unchecked. It gets checked back on when you press f5.

  • @anantakesharipanda4085
    @anantakesharipanda4085 3 дня назад +1

    Hey Navdeep! I was watching your video on Trie in your Advanced algo course yesterday to solve *Problem **#1032* , hence that’s where my first intuition went for this problem as well. But then I thought, “No way that would make this an easy problem.”

  • @clementjean4905
    @clementjean4905 3 дня назад +5

    Another approach to consider is sorting the words by size (substring size

    • @prathamesh-bhosale
      @prathamesh-bhosale 3 дня назад +1

      Yes that was my first thought I was honestly surprised it was better than brute force approach. One more optimization I found was to break once a match was found for words[i] it avoids going all the way to end of the array each time. Good catch on the same length words didn't really think of that!!

    • @clementjean4905
      @clementjean4905 3 дня назад +1

      @prathamesh-bhosale the break is required I think, otherwise you could have duplicates. Did I misunderstand what you meant?

    • @ListeningTheWind
      @ListeningTheWind 3 дня назад

      @@clementjean4905 could you tell me what is the time complexity of the approach you mentioned above

    • @clementjean4905
      @clementjean4905 3 дня назад

      @@ListeningTheWind the sorting is O(N Log N) and the checking of the string should be less than O(N^2 * M). I'm not entirely sure on how to calculate that properly though. If anyone has insights on this, I'm interested.

    • @ukeNlife
      @ukeNlife 3 дня назад +1

      Yeah. I also though of this

  • @avishjain7375
    @avishjain7375 3 дня назад

    Correct me if wrong, sorting the words by size we can find if a is substring of b (a

    • @staywithmeforever
      @staywithmeforever 3 дня назад

      @@avishjain7375 in python it doesn't matter

    • @staywithmeforever
      @staywithmeforever 3 дня назад

      And still u need the indexes, the answer should be in low to high index format

  • @Code_Leveler
    @Code_Leveler 3 дня назад

    Thanks a lot sir 😁 , I tried to solve it with Rabin karp it was pretty difficult for me as i had to revise the concept to solve the problem
    Here's the code => Rabin Karp
    class Solution {
    public final static int d=256;
    public List stringMatching(String[] words) {
    Set res=new HashSet();
    int q=101;
    for(int i=0;i

  • @moralized
    @moralized 3 дня назад +9

    1:00 "maas" :D

    • @NeetCodeIO
      @NeetCodeIO  3 дня назад

      Something something broken clock ⏰ 😂

  • @3thmnify
    @3thmnify 2 дня назад

    You convert each word into a number which is the product of its letters. Then divide each word by every other word and check if modulus 0, if so compare the actual strings.
    This gives you O(n) space and O(n * L) time.
    The time complexity is actually slightly more, due to the possibility of collisions, but not by much. You could do some character analysis to decrease the factorization, and maintain a mapping of each character to its “reduced” number, which will take O(L) space, but reduce the number of collisions.

  • @vierliu5322
    @vierliu5322 3 дня назад

    i implemented a trie extended with a special lookup function for substring, but it turns out to be less performant than the easy nested loop implementation.
    Substring search on trie will visit the whole tree on a search miss, degenerating the implementation to O(n^2) 😂

  • @boxicool
    @boxicool 3 дня назад

    Noob question. Where should i start with such exercies? I know basic syntax of python.

  • @santhu-hw2qw
    @santhu-hw2qw 3 дня назад

    DONE

  • @krunalkp1032
    @krunalkp1032 2 дня назад

    o(n) Solution No KMP and No Rabin Karp
    combine all words by ' ' space
    Now search every word count is > 1 or not
    Code :
    class Solution:
    def stringMatching(self, words: List[str]) -> List[str]:
    sentence = ' '.join(words)
    answer = []
    for i in words:
    if sentence.count(i) > 1:
    answer.append(i)
    return answer

    • @NeetCodeIO
      @NeetCodeIO  2 дня назад

      Is this actually more efficient? Doesn't it just move the complexity to the count operation which now happens over the sentence and still does the same number of comparisons?

    • @krunalkp1032
      @krunalkp1032 2 дня назад

      ​@@NeetCodeIO It shows me 100% while leetcode submission, but I guess worst time complexity would be O(n*len(word))
      My bad, word in sentence TC is O(n) in python but
      word count in sentence TC is O(n*len(word))
      I overlooked it.

    • @codewithven9391
      @codewithven9391 2 дня назад

      @@krunalkp1032 you are counting the number of time the word appears in the entire string each time. The time complexity is the same as brute force ie O(n^2 * L^2). Just because you're getting a 100% doesn't mean the time complexity is better.

    • @alizu3803
      @alizu3803 2 дня назад

      same time complexity bro

  • @STACYEMMA-y2e
    @STACYEMMA-y2e 2 дня назад

    Brute forced again , today's problem

  • @business_central
    @business_central 3 дня назад +3

    I spent my time thinking of Trie... you can laugh at me everyone I messed up

    • @flamendless
      @flamendless 3 дня назад +10

      Good trie

    • @anantakesharipanda4085
      @anantakesharipanda4085 3 дня назад

      So did I, my first thought was, “How is this an easy problem?” Efficient substring search algorithms can be complicated to implement.
      But implementing Trie in this problem will definitely make it a hard problem, just like *problem **#1032* .

    • @floriankubiak7313
      @floriankubiak7313 3 дня назад +1

      The trie implementation is part of the editorial. It's not a mess-up - it's a good implementation for boundary conditions that have lots of lookups.

    • @business_central
      @business_central 2 дня назад

      ​@@flamendless love this 🤣

    • @business_central
      @business_central 2 дня назад +1

      @@floriankubiak7313 oh really? I need to check that out, it was very hard and I couldn't come up with any solution

  • @yang5843
    @yang5843 3 дня назад +1

    An easy one

    • @freecourseplatformenglish2829
      @freecourseplatformenglish2829 3 дня назад +4

      Untill interviewer ask you to optimize solution. he is expecting you to use KMP or Rabin-karp algorithm.

    • @JamesBond-mq7pd
      @JamesBond-mq7pd 3 дня назад

      @@freecourseplatformenglish2829 Ah yes, because every good interview is just a subtle test to see if you can read their mind and whip out KMP or Rabin-Karp on cue. Who cares about problem-solving steps when you could just impress them with algorithm trivia instead. 🫡💀