Lets start a new trend, please comment "understood" in case you did :)
@siddharthsingh99128 күн бұрын
Always avoided Trie concept, but after watching your videos I have started thinking if a particular problem can be solved using Trie DS or not. Haha ! Great Content, keep it up..
@shreyanshmittal9381 Жыл бұрын
Understood it very well. Pretty simple implementation, was able to code it myself upon seeing the explanation! Thanks a lot raj bhaiya❤
@sakshampandit1335 Жыл бұрын
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..
@techdemy40502 жыл бұрын
@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
@sohamsen78922 жыл бұрын
Same query
@thinkingmad16852 жыл бұрын
Use map then worst case will be nlogn
@_-69122 жыл бұрын
understood and its clever way of implementation.
@vidhishah91542 жыл бұрын
Understood, Thanks for making hard things easy.
@lavanya_m017 ай бұрын
Brilliant approach!
@your_name962 жыл бұрын
Imp : if multiple words are not given, no need to construct explicit trie class
@abhayj87062 жыл бұрын
Please start dp series only you can teach best 🙏🙏🙏🙏💯
@yogendrajhala41472 жыл бұрын
Bro check aditya dp series, it will help u
@Rohitkumar-ks9io2 жыл бұрын
@@yogendrajhala4147 Yes I agree!! Aditya Verma is like DP God
@satyampande6842 жыл бұрын
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?
@takeUforward2 жыл бұрын
Hashing
@sarthak74382 жыл бұрын
I'm not getting tle with trie approach
@satyampande6842 жыл бұрын
@@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.
@rushidesai28363 ай бұрын
Thanks Striver!
@UECAshutoshKumar7 ай бұрын
Thank you 🙏
@mohitsingh7793 Жыл бұрын
Avoid String hashing it will give u MLE when string length is very large.
@nishankdeep30842 жыл бұрын
understood bhaiya thank you bhaiya
@bhai-bm4kj Жыл бұрын
Understood bhaiya
@coderhat19802 жыл бұрын
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?
@azeezmoiz91952 жыл бұрын
use suffix arrays
@tukaighosh43832 жыл бұрын
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.
@takeUforward2 жыл бұрын
Unordered set has, not set
@mevam1232 жыл бұрын
So why we used set here and not the unordered set?
@tusharvlogs6333 Жыл бұрын
@@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 Жыл бұрын
@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.
@priyanshkumar177 ай бұрын
@@tusharvlogs6333 yes
@mohitsanjaymahajan324624 күн бұрын
Using unordered_set it will be N^2 so i think no need to use trie?
@anujtyagi40472 жыл бұрын
use unordered set instead of set
@wolfrikz72382 жыл бұрын
understood, great explanation ❤️
@manoharmanu92402 жыл бұрын
Understood 🙌
@Harsh-pv7pb Жыл бұрын
Is there any O(n) solution present for this??
@SurajSingh-kd8gw Жыл бұрын
Is creating trie class important ? Like in last question u did it, so can we solve that without creating it
@sanjuchandra94057 ай бұрын
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 } };
@hemantvardani14362 жыл бұрын
Thanks
@utkarsh_1082 жыл бұрын
understooooood. thanks bhaiya 🙂
@devendrapatil072 жыл бұрын
Node* links[26] ; These array has default value of NULL?
@shubhrajit21174 ай бұрын
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.
@tusharvlogs6333 Жыл бұрын
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 Жыл бұрын
Its basically less than equal to n - 1 for(int j = 0; j
@Electrocode_gk8 ай бұрын
i =0 bakwas
@sivaganesh44892 жыл бұрын
Thanks dude
@arihantjammar8888 Жыл бұрын
Understood 😊
@bhai-bm4kj Жыл бұрын
Love you sir
@tanujsharma28742 жыл бұрын
can you plz tell me the board name you are using ?
@Yash-uk8ib2 жыл бұрын
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?
@vishalgupta9572 жыл бұрын
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 :)
@abhinavsingh65322 жыл бұрын
@@vishalgupta957 but to insert a string in unordered_set take O(1) then it must be O(n^2) only ?
@vishalgupta9572 жыл бұрын
@@abhinavsingh6532 nope it's amortized O(1)
@jolly_dollyyy2 жыл бұрын
understood!!
@ashishsachan082 жыл бұрын
Great...Thanks..
@arahul60982 жыл бұрын
understood..
@anshulgoel1940 Жыл бұрын
Understood but why do we need a set here to strings at 4:11
@sohamsen78922 жыл бұрын
Understood
@shubhamkumar1305 Жыл бұрын
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.
@Agent47_live Жыл бұрын
what teaching app are you using?
@anuragsahay99442 ай бұрын
an OA asked to solve it in O(NLogN). Len(string) was 1e5.
@AmandeepSingh-uq3wp2 жыл бұрын
understood
@satwiktatikonda764 Жыл бұрын
how the worst case tc for adding to set takes logm..if there are collisions it becomes o(m) right ? can anyone explain
@greedycat98452 жыл бұрын
understood😁
@satyampande6842 жыл бұрын
Understood!!
@sagarbora77682 жыл бұрын
same content i learned in masterclass
@aakashparmar5602 жыл бұрын
Understood!!!
@DebashishGhoshOfficial2 жыл бұрын
The average length of a sub-string of a non-empty string of length n is (n + 2) / 3.
@remmargorpp2 жыл бұрын
wouldn't that be word[j] in the second for loop?
@takeUforward2 жыл бұрын
You can check the github code once… its running and accepted one
@remmargorpp2 жыл бұрын
@@takeUforward yep the github code is errorless! Thank you for the reply.
@takeUforward2 жыл бұрын
@@remmargorpp yes since i was typing live, might just have done a typo. Thanks.
@champion59462 жыл бұрын
UnderStood
@souravchakraborty97772 жыл бұрын
Other than TRIE how to solve this problem? Would you kindly tell me the answer?
@Sandeepg2552 жыл бұрын
via suffix array
@rahulsrivastava97102 жыл бұрын
Time limit exceeded... ???
@visheshagrawal86767 ай бұрын
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; }
@AlokYadav-SKB2 жыл бұрын
Ask your viewers to click on youtube Icon/Text then only they would be able to see Like and comment option.
@addityasharma64262 жыл бұрын
understood :-)
@shradhaydham47456 ай бұрын
why don't we need trie class here?
@vanshchaudhary8291 Жыл бұрын
US
@shivampathak13712 жыл бұрын
In code, line 4
@mkat21 Жыл бұрын
Sir question link thoda ur chupa Kar rakh date
@himanshuagarwal10662 жыл бұрын
Bro you mentioned i and putting index i instead of j that was wrong bro
@GyaneshTiwari-dw4zb Жыл бұрын
understood ++
@akashsahu25717 ай бұрын
cfbr
@amborishnath77552 жыл бұрын
us
@ssss-mv6dx2 ай бұрын
Why are we doing the get? what is the need to check the containsKey?
@user-tj3us8il6f2 жыл бұрын
**********Where r more opportunities for a tier 3 college guy(in terms of getting interview call) in well funded start ups or big MNCs***********
@vishalgupta9572 жыл бұрын
level up your DSA you will get interview calls from both. Happy Coding :)