Longest common prefix

  Рет қаралды 79,676

Techdose

Techdose

4 жыл бұрын

This video explains two simple and efficient methods to solve the longest common prefix problem by using simple loops and second method uses trie. The code link is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK (1st method): www.geeksforgeeks.org/longest...
CODE LINK (Trie): www.geeksforgeeks.org/longest...

Пікірлер: 56
@Hav0c1000
@Hav0c1000 4 жыл бұрын
Trie method is slower in the worst case vs Brute Force, and uses O(n) space... why use the trie then?
@Hav0c1000
@Hav0c1000 4 жыл бұрын
Nice explanation, btw
@techdose4u
@techdose4u 4 жыл бұрын
No dude. Worst case is much slower. It takes O(NoOfWords * TimewithTrie). Just think it over. We are comparing all words in brute force.
@Hav0c1000
@Hav0c1000 4 жыл бұрын
TECH DOSE You state the time complexity as O(n*len(smallestString)) for the first method, and O(n*len(string)) in the second method. Seems like the first one is better.
@techdose4u
@techdose4u 4 жыл бұрын
N is no of strings. In the 1st method you have O(noOfStrings*len(string)) but in trie method...O(noOfStrings*len(string)) for building trie. After it is built, then, O(noOfWords having same prefix * len(string - prefixLength)), since we don't need to match prefix more than once. As no of queries increases, you will see the power of tri. I hope it's clear now :)
@Hav0c1000
@Hav0c1000 4 жыл бұрын
@@techdose4u I see what you are saying. Thanks for taking the time to discuss!
@ambikakanakkur5902
@ambikakanakkur5902 3 жыл бұрын
Thank you, your videos help me to understand problems very quickly. It's precise and crisp.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@niveditha-7555
@niveditha-7555 3 жыл бұрын
One day this channel is gonna hit 100k subs and then I will be bragging about how helpful u were!!
@techdose4u
@techdose4u 3 жыл бұрын
❤️
@bibs24
@bibs24 2 жыл бұрын
It has hit 100k and he deserves it.
@bismaypurkayastha8313
@bismaypurkayastha8313 4 жыл бұрын
The trie problem can be solved in this manner also: We look for the smallest word in the input array and then form a trie with that word only... Suppose the words are apple ape and apps.. Then we form a trie with ape only a(wc = 1), p(wc = 1) , e(wc = 1)..then we check for others words.. For apple we check a (wc = 2) p( wc = 2 ) and then as e and p are not matching we stop there and go for the next word apps a(wc = 3) p(wc = 3) and then we finally stop.. Then we traverse from the root to extract the longest common prefix (upto that node whose wc = length of input array) Correct me if i am wrong.. :)
@shubhammishra3783
@shubhammishra3783 4 жыл бұрын
Very clear explanation thank you :)
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@kabboghosh1853
@kabboghosh1853 4 жыл бұрын
GREAT teaching sir love from bangladesh
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sathwikkunduru5503
@sathwikkunduru5503 4 жыл бұрын
Even though this takes a bit more time we can move further to O(nlogn) efficiency After sorting the string array -O(nlogn) We can check the first string and last string of the array -O(n) O(nlogn) + O(n) Might reduce complexity a lot space as well as time
@vaibhavjsr
@vaibhavjsr 4 жыл бұрын
when you're sorting an array of numbers, the complexity is O(nlogn) because comparing two numbers takes O(1) time [for example 2>1 takes O(1) time] But when sorting an array of strings, you'll be comparing two strings at each step. And if length of all strings is say 'm' your time complexity will become O(m*nlogn) Hence sorting might not help us out here
@mohammadosama4709
@mohammadosama4709 3 жыл бұрын
How that's supposed to make sense?
@chetanraghavv
@chetanraghavv Жыл бұрын
you only need minimum and maximum strings so instead of sorting, just traverse. Sorting method: m*nlogn Traversing method: m*n
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Premiere dekha maine 🎉
@shivamsahil3660
@shivamsahil3660 Жыл бұрын
method 1 which is kind of a brute force method but I like this method 😂 I love that method
@alxx736
@alxx736 3 жыл бұрын
What software do you use to explain?
@mysteriousocean3206
@mysteriousocean3206 3 жыл бұрын
lovely explanation sir
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@bob_factory
@bob_factory 4 жыл бұрын
The Brute force method 1 is kinda better I think. It does screening of each and every element and break accordingly. Consider the following input of strings to compare: ["Carbonate", "Carosal", "Car"]. If we use method 1 we will break out immediately after "Car" words length is reached. Whereas if we use trie we will be iterating through the entire 1st word, entire 2nd word and then entire 3rd word. Using 1 method, We traverse through only 1st three characters of all the strings and then break out . By the way is there any other algorithm to solve string comparisons. I am hunting algorithms for this problem.
@techdose4u
@techdose4u 4 жыл бұрын
Actually trie may not seem efficient to you right now. But consider larger test cases with thousands of strings and each string being pretty long. If you keep inserting strings in a trie, huge number of strings can be accommodated in a small space. Afterwards if you wanna find longest common prefix or any specific count of strings with given prefix. It will just take O(prefix_length) TIME. In this case, bruteforce will take O(Prefix_length*NoOfStrings) TIME which is very high. So, you see TRIE is one of the best if not the best.
@treyquattro
@treyquattro 3 жыл бұрын
@@techdose4u seems you're inventing reasoning here - finding prefixes after the fact - that wasn't part of the original problem description. This channel is very good on these kinds of problems, I just don't see an improvement over best brute force case in this instance (finding minimum length of all strings while checking first character)
@fb_a
@fb_a 2 жыл бұрын
@@techdose4u got it. If there is a need to persist the strings then trie is better choice to go with. Otherwise we can go with brute force.
@rajbopche7992
@rajbopche7992 3 жыл бұрын
Is the Trie approach Backtracking ??
@dattatreyapujar4068
@dattatreyapujar4068 4 жыл бұрын
Good work.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sp2246
@sp2246 4 жыл бұрын
Thanks Bro!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@Akash_Ghosh256
@Akash_Ghosh256 11 ай бұрын
Happy teachers day Bhaiya
@Vendettaaaa666
@Vendettaaaa666 3 жыл бұрын
Apparently, you don't need to maintain any counters. We just traverse until each node has children of size 1.
@shubhammishra3783
@shubhammishra3783 4 жыл бұрын
Please make more on Word Break and Word boggle problem
@techdose4u
@techdose4u 4 жыл бұрын
Okay sure.
@adityapahujavines3684
@adityapahujavines3684 Жыл бұрын
this code is much better then your first method code as both of them are o(n^2) tc and o(1) sc class Solution { public: string longestCommonPrefix(vector& strs) { //TC - O(N ^ 2) // SC - O(1) string ans = ""; for(int i=0;i
@jatinbhatoya8420
@jatinbhatoya8420 3 жыл бұрын
sir this is recursive method. can you please tell the mistake. class Solution { public static String longestCommonPrefix1(String[] strs, int index) { if (index < 0) { return ""; } if (index == 0) { return strs[index]; } String st = longestCommonPrefix1(strs, index - 1); StringBuffer sb = new StringBuffer(); for (int i = 0; i < Math.min(st.length(), strs[strs.length-1].length()); i++) { if (st.charAt(i) == strs[strs.length - 1].charAt(i)) { sb.append(st.charAt(i)); } else { return sb.toString(); } } return sb.toString(); } public static String longestCommonPrefix(String[] strs) { return longestCommonPrefix1(strs, strs.length - 1); } }
@HimanshuSingh-ob9qo
@HimanshuSingh-ob9qo 3 жыл бұрын
👍
@techdose4u
@techdose4u 3 жыл бұрын
👍
@ronitroy1391
@ronitroy1391 2 жыл бұрын
class Solution { public: string longestCommonPrefix(vector& strs) { int i=0,j=0,count=0,min=INT_MAX; string s; while(i
@JatinKumar-hm7iw
@JatinKumar-hm7iw 3 жыл бұрын
can anyone pls tell me how first method is applicable to apple, mapple , creamapple ??
@MrACrazyHobo
@MrACrazyHobo 3 жыл бұрын
It's looking for prefix, not substring, so the answer for your example would be "".
@JatinKumar-hm7iw
@JatinKumar-hm7iw 3 жыл бұрын
Oh my bad... it's prefix... you are correct..!
@treyquattro
@treyquattro 3 жыл бұрын
yeah this one makes no sense. The trie version is much more inefficient in terms of space - brute force requires no extra space - and in the best case scenario is the same time complexity as brute force - number of strings * length of shortest prefix when shortest prefix is the length of the shortest string
Kadanes algorithm | Longest sum contiguous subarray
7:51
Techdose
Рет қаралды 159 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 23 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 13 МЛН
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 116 МЛН
Comfortable 🤣 #comedy #funny
00:34
Micky Makeover
Рет қаралды 14 МЛН
MAGNUS vs HANS ANNOUNCEMENT!!!!!!!
28:17
GothamChess
Рет қаралды 264 М.
Lecture 80: Longest Common Prefix Problem || Tries || C++ Placement Series
20:17
MAGNUS IS PERFECTION!!
20:15
GMHikaru
Рет қаралды 112 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 23 МЛН