L4. Number of Distinct Substrings in a String | Trie | C++ | Java

Поділитися
Вставка
  • Опубліковано 25 лис 2021
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------------------------------- Lecture notes/C++/Java Codes - takeuforward.org/data-structu...
    Pre-requisite: First Video of the Playlist: • L1. Implement TRIE | I...
    In this video, we discuss the question counting the number of distinct substrings in a string.
    Please share this channel as much as you can. Also, it's an earnest request to drop a comment "understood" if you are watching this video, so that I get to know you understood from this video.
    SDE-Sheet/Linkedin/Telegram/Instagram: linktr.ee/takeUforward
    -------------------------------------------------------------------------------------------------------------
    Problem Link: bit.ly/3ocRQW0

КОМЕНТАРІ • 126

  • @takeUforward
    @takeUforward  2 роки тому +30

    Lets start a new trend, please comment "understood" in case you did :)

  • @shreyanshmittal9381
    @shreyanshmittal9381 Рік тому +4

    Understood it very well. Pretty simple implementation, was able to code it myself upon seeing the explanation! Thanks a lot raj bhaiya❤

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

    Understood, Thanks for making hard things easy.

  • @_-6912
    @_-6912 2 роки тому +2

    understood and its clever way of implementation.

  • @lavanyam3224
    @lavanyam3224 4 місяці тому

    Brilliant approach!

  • @abhayj8706
    @abhayj8706 2 роки тому +18

    Please start dp series only you can teach best 🙏🙏🙏🙏💯

    • @yogendrajhala4147
      @yogendrajhala4147 2 роки тому +5

      Bro check aditya dp series, it will help u

    • @Rohitkumar-ks9io
      @Rohitkumar-ks9io 2 роки тому +3

      @@yogendrajhala4147 Yes I agree!! Aditya Verma is like DP God

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

    understood, great explanation ❤️

  • @sakshampandit1335
    @sakshampandit1335 Рік тому +6

    sir there is a mistake in your code "instead of word[i] you should have written word[j] in every function you have passed" it will not execute properly..

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

    Great...Thanks..

  • @techdemy4050
    @techdemy4050 2 роки тому +5

    @take U forward what if I use unordered_map mp instead of using links[26] does space complexity will be O(n^2) in the worst case scenario. Please correct me if I am wrong

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

    understood!!

  • @rushidesai2836
    @rushidesai2836 22 дні тому

    Thanks Striver!

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

    understood bhaiya
    thank you bhaiya

  • @mohitsingh7793
    @mohitsingh7793 Рік тому +5

    Avoid String hashing it will give u MLE when string length is very large.

  • @manoharmanu9240
    @manoharmanu9240 Рік тому +1

    Understood 🙌

  • @UECAshutoshKumar
    @UECAshutoshKumar 4 місяці тому +1

    Thank you 🙏

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

    Thanks dude

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

    understooooood. thanks bhaiya 🙂

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

      Node* links[26] ;
      These array has default value of NULL?

  • @your_name96
    @your_name96 Рік тому +3

    Imp : if multiple words are not given, no need to construct explicit trie class

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

    Understood!!!

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

    Understood!

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

    Understood!!

  • @bhai-bm4kj
    @bhai-bm4kj Рік тому

    Understood bhaiya

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

    understood great explaination. But I have one more doubt I am able to submit the answers , but its showing TLE. can we reduce the O(n2) complexity?

  • @shubhamkumar1305
    @shubhamkumar1305 Рік тому +1

    Sir, there is something wrong during you write code same code I written in python , but we try test case "ninja" it should be return 15 (14 + 1), but answer is not getting right.

  • @satyampande684
    @satyampande684 2 роки тому +11

    I tried to solve the problem given in the problem link but I am getting TLE as a verdict and in question, it is specifically written that we have to implement the trie method. Is there any more approach better than this one?

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

      Hashing

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

      I'm not getting tle with trie approach

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

      @@sarthak7438 Yes, I think they have increased time limit or decreased test cases. Same code got TLE 6th months before but now it's showing correct answer.

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

    understood😁

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

    Thanks

  • @arihantjammar8888
    @arihantjammar8888 9 місяців тому

    Understood 😊

  • @bhai-bm4kj
    @bhai-bm4kj Рік тому

    Love you sir

  • @AmandeepSingh-uq3wp
    @AmandeepSingh-uq3wp 2 роки тому

    understood

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

    Understood

  • @satwiktatikonda764
    @satwiktatikonda764 9 місяців тому

    how the
    worst case tc for adding to set takes logm..if there are collisions it becomes o(m) right ? can anyone explain

  • @chandrachurmukherjeejucse5816

    Understood.

  • @tukaighosh4383
    @tukaighosh4383 2 роки тому +2

    Understood.. But couldn't understand why you mentioned time complexity of set.add() as logN at 4:11 . As we know that HashSet has amortized time complexity as O(1) for add operation as it uses hashmap internally. I'm assuming that hash collision will rarely occur.
    Again I would say you explained it very well.

    • @takeUforward
      @takeUforward  2 роки тому +2

      Unordered set has, not set

    • @mevam123
      @mevam123 Рік тому +2

      So why we used set here and not the unordered set?

    • @tusharvlogs6333
      @tusharvlogs6333 Рік тому +1

      @@mevam123 absolutely what i was thinking of. we only need the substrings, order doesn't matter to us so we can use unordered_set

    • @ar3568row
      @ar3568row 10 місяців тому +2

      @adolft_official and so is the case with Trie, I think Trie doesn't improve the time complexity over unordered_set, it only improves the space complexity.

    • @priyanshkumariitd
      @priyanshkumariitd 4 місяці тому

      @@tusharvlogs6333 yes

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

    can you plz tell me the board name you are using ?

  • @Harsh-pv7pb
    @Harsh-pv7pb Рік тому +2

    Is there any O(n) solution present for this??

  • @anujtyagi4047
    @anujtyagi4047 2 роки тому +2

    use unordered set instead of set

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

    understood..

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

    understood :-)

  • @tusharvlogs6333
    @tusharvlogs6333 Рік тому +2

    why did the loop run till n-1 for the brute approach. shouldn't it run till n times. because we also need to consider the last ele of the string.

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

      Its basically less than equal to n - 1 for(int j = 0; j

    • @Coder_421
      @Coder_421 5 місяців тому

      i =0 bakwas

  • @SurajSingh-kd8gw
    @SurajSingh-kd8gw Рік тому

    Is creating trie class important ? Like in last question u did it, so can we solve that without creating it

  • @anshulgoel1940
    @anshulgoel1940 10 місяців тому

    Understood but why do we need a set here to strings at 4:11

  • @shubhrajit2117
    @shubhrajit2117 Місяць тому

    At 4:06 I searched the time complexity of set operations and it said O(1) for avg case and O(N) for worst case.

  • @DebashishGhoshOfficial
    @DebashishGhoshOfficial 2 роки тому +2

    The average length of a sub-string of a non-empty string of length n is (n + 2) / 3.

  • @Yash-uk8ib
    @Yash-uk8ib 2 роки тому +1

    sir, why is space complexity of the brute soln O(n*n) * O(n) ? why is string length taken into account??
    why is it not correct to say that it takes O(n*n) space?

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

      because the average length of substring is n/2 on an average. there can be substring of size 1 as well and substring of size n as well so it averages out to be n/2 for each substring.
      Cheers :)

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

      @@vishalgupta957 but to insert a string in unordered_set take O(1) then it must be O(n^2) only ?

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

      @@abhinavsingh6532 nope it's amortized O(1)

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

    same content i learned in masterclass

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

    what teaching app are you using?

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

    Other than TRIE how to solve this problem? Would you kindly tell me the answer?

  • @sanjuchandra9405
    @sanjuchandra9405 3 місяці тому

    for those who are getting memory limit error in leetcode:
    const int MAX_NODES = 500 * 500; // Maximum number of nodes in the trie (based on the problem constraints)
    int trie[MAX_NODES + 5][26]; // Trie data structure: Each node can have up to 26 children (corresponding to letters 'a' to 'z')
    class Solution {
    public:
    int countDistinct(string &str) {
    int n = str.size(); // Length of the input string
    int ans = 0; // Count of distinct substrings
    int sz = n * n * 26 * sizeof(int); // Size of memory to initialize trie
    memset(trie, 0, sz); // Initialize trie with zeros
    int root = 0; // Root of the trie
    int temp = root; // Temporary variable to traverse the trie
    // Loop over all substrings of the input string
    for (int i = 0; i < n; i++) {
    // Start building substrings from index i
    for (int j = i; j < n; j++) {
    int d = str[j] - 'a'; // Calculate the index corresponding to the character
    // Access the child node 'd' of the current node 'temp' in the trie
    int &pos = trie[temp][d];
    if (pos == 0) {
    // If the child node doesn't exist (not visited before), create a new node
    ans++; // Increment the count of distinct substrings
    pos = ans; // Link the new node with its parent
    }
    // Move to the next node in the trie (corresponding to the character 'd')
    temp = pos;
    }
    // Reset temp to root for the next substring starting from index i+1
    temp = root;
    }
    return ans; // Return the total count of distinct substrings
    }
    };

  • @GyaneshTiwari-dw4zb
    @GyaneshTiwari-dw4zb Рік тому

    understood ++

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

    Time limit exceeded... ???

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

    UnderStood

  • @vanshchaudhary8291
    @vanshchaudhary8291 11 місяців тому

    US

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

    In code, line 4

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

    wouldn't that be word[j] in the second for loop?

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

      You can check the github code once… its running and accepted one

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

      @@takeUforward yep the github code is errorless! Thank you for the reply.

    • @takeUforward
      @takeUforward  2 роки тому +2

      @@yogeshvaishnav7856 yes since i was typing live, might just have done a typo. Thanks.

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

    us

  • @mkat21
    @mkat21 9 місяців тому

    Sir question link thoda ur chupa Kar rakh date

  • @shradhaydham4745
    @shradhaydham4745 3 місяці тому

    why don't we need trie class here?

  • @AlokYadav-SKB
    @AlokYadav-SKB Рік тому

    Ask your viewers to click on youtube Icon/Text then only they would be able to see Like and comment option.

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

    Bro you mentioned i and putting index i instead of j that was wrong bro

  • @akashsahu2571
    @akashsahu2571 4 місяці тому

    cfbr

  • @LowkeyCoder
    @LowkeyCoder 7 місяців тому

    "Understood"

  • @user-tj3us8il6f
    @user-tj3us8il6f 2 роки тому

    **********Where r more opportunities for a tier 3 college guy(in terms of getting interview call) in well funded start ups or big MNCs***********

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

      level up your DSA you will get interview calls from both.
      Happy Coding :)

  • @visheshagrawal8676
    @visheshagrawal8676 3 місяці тому

    working code without trie if someone needs :
    #include
    int countDistinctSubstrings(string &s)
    {
    int ans = 0;

    unordered_setst;
    for( int i = 0 ; i < s.size() ;i++){
    string temp = "";
    for( int j = i ; j < s.size() ; j++){
    temp +=s[j];
    st.insert(temp);
    }

    }
    return st.size()+1;
    }

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

    Understood!

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

    understood

  • @VinayKumar-ze2ww
    @VinayKumar-ze2ww 2 роки тому

    Understood

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

    us

  • @codingp110
    @codingp110 Місяць тому

    Understood!

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

    Understood

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

    Understood

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

    understood

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

    understood

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

    understood

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

    understood

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

    Understood

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 Рік тому +1

    understood

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

    Understood

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

    Understood

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

    understood

  • @komalyadav5599
    @komalyadav5599 11 місяців тому

    Understood

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 7 місяців тому

    Understood

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

    understood

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

    understood

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

    understood

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

    understood

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

    understood

  • @javierdavidhernandezestrad7334
    @javierdavidhernandezestrad7334 4 місяці тому

    understood

  • @manideepdeepnaidugorle7308
    @manideepdeepnaidugorle7308 Місяць тому

    understood

  • @shaiksoofi3741
    @shaiksoofi3741 26 днів тому

    understood

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

    us