A concise and effective demonstration of how Trie structures transform quadratic bottlenecks into linear efficiency. It is a quintessential example of algorithmic optimization that every serious developer should internalize.
Deep Dive
Voraussetzung
- Keine Daten verfügbar.
Nächste Schritte
- Keine Daten verfügbar.
Deep Dive
Find the Length of the Longest Common Prefix | LeetCode 3043 - PythonIndiziert:
LinkedIn: https://www.linkedin.com/in/pabloolle/ Discord: https://discord.gg/d3AxpBe9eD This video is not affiliated with or endorsed by LeetCode LLC. All problem references are for educational purposes only.
Hello everyone. I'm Paul and this is problem 3043.
Find the length of the longest common prefix. This is a medium problem that might become tricky if you don't know how to build and use a try. So, let's start.
This is a nice description. You can read it if you want. But basically, we have to return the length of the longest common prefix among all pairs. We are given these two arrays. Here, the longest common prefix is given by these two integers. And more specifically, the longest common prefix is 100. The length of this longest common prefix is three.
So, the result in this case is three.
The naive solution is the following. We can say that the result is initially zero because we are looking for the longest common prefix. And now we can go through every possible pair in these two arrays. First, we take the pair 1 1000.
Here, the longest common prefix is one.
And the length of this longest common prefix is one. Given that one is greater than zero, we can replace this with one.
Next, we can try the pair 10 1000. Here, the longest common prefix is 10. The length of this is two. And two is greater than one. So, the result now is two. Finally, we can take the pair 100 1000. Here, the longest common prefix is 100. And the length of this prefix is three. Given that three is greater than two, the result is three. Now, this works technically. But down here we have the constraints and we know that these two arrays can have lengths of up to 5 * 10 ^ 4. And if we iterate every possible pair, this requires roughly big O of n squared. It's actually a bit more because we are transforming every integer into a string. And this number to the power of two is a a large number which we can't handle. So, we have to think about something else. The key here is that we can use something called a trie. So, let's see how this works. This is the final trie if we decide to add every number in array one. So, let's see how to build this.
Let's say that we are trying to add this number into the trie. We always start from the root, and for every node we have different amounts of children. We can have zero or more. In this case, we have zero.
So, we are trying to add this. First, we check if we have this number as a child.
Given that we don't, we add it.
And that's it. Now, we have successfully inserted this number. Now, we can continue with this one. So, we start from the root every time, and we are going character by character, or in this case, digit by digit. We start from index zero, so first, the I pointer is here. We start with one. We are checking if from the root, we have a child that's equal to one, and we have it right here.
So, now we are standing at this node, and we can move the I pointer one step to the right. Next, we are here, and the current digit is zero. So, we are checking if from this node, we have a child that's equal to zero. Given that we don't, we need to build a node with a value of zero.
Now, we have successfully inserted this value, and we can continue with the final one. We start with the I pointer at the first digit. We start from the root in the trie, and we are checking if we have a one as a child. Given that we do, we can continue. Now, the I pointer is here, and we are checking if from this point, we have zero as a child. And we do have zero, so we continue. Now, the I pointer is here, and from here, we don't have another zero as a child, so we add it.
And now that we have this, we can go through every value in array two and check what's the longest common prefix.
So, we can say that the result is initially zero and here we only have one element. So, we are doing something very similar. We go digit by digit starting from index zero and we will keep track of the current common prefix with a value which we'll call count. This is initially zero. And we always start from the root. So, we are checking if from this node we have a child that's equal to one and we do have one. So, we can increase one here. The count is one.
And we can continue with the next index.
Now we need a zero and from this node we have zero as a child, so we add one to the count. This is two and we move the I pointer.
Now the I pointer is here. So, from this node we are checking if we have zero as a child and we do have zero. So, we add one to the count and move the I pointer.
Finally, the I pointer is here and we are standing at this node. Given that we don't have zero as a child, we can break the loop now and the maximum common prefix so far is three. Given that we only have one value here, that's it. The result is the maximum between zero and three, which is three. So, we return this.
And by doing this, we are solving the problem in roughly big O of M plus M.
Why? Well, because first we need to add all these values into the trie. Remember that for each number, we are going through every digit one by one and at the same time we are iterating these nodes. So, overall time complexity for inserting each value is roughly big O of one because the numbers are relatively small. So, overall time complexity for inserting the values in array one is roughly big O of N. And then we have to go through the values in array two to check what's the longest common prefix.
So, this is overall time complexity and overall space complexity is roughly big O of N because remember that we start by adding these values into the tri. So, the space is given by this.
In the worst case, we have no shared prefixes. For example, we can have 3 5 7 and so on. Every prefix is different and in this case overall space is big O of N. So, with this in mind, now let's go with the code. First, we need to build two classes for the tri. We will call the first one tri node.
So, here we need a constructor. In Python, we use init and this takes self and some value. We will say that this is an empty string by default. Now, here we will say that self. children is an empty map and self.value is the value that we are giving here.
Now, we can build another class for the tri.
This requires another constructor. So, here we use init again and this takes self.
Here we will say that self.root is equal to a new tri node.
Now, we need two methods. We will call the first one insert. This takes self and some value. This is how we add numbers.
And here we say that current is equal to self.root.
And we can say for every character in the value, if we don't have the character in current.children, sorry, this is children.
This means that we need to build the new node. So, current.children at C is now equal to a new trie node using C as the value.
And every time we need to move to the next node. Remember the drawing. So, we say that current is equal to current dot children at C. And that's it for insert.
Now, we can move to the next method, which is max prefix. So, this takes self and some value.
Remember that we are using this for array two.
Here, we do something very similar.
Current is equal to self dot root.
We can say that count, sorry, count is equal to zero.
And for every character in the value that we are given, if we don't have this character in current dot children, we can break the loop right away and return count. Else, if we are here, we can say that current is equal to current dot children at C.
And we add one to the count.
After all of this, we can just return count. Sorry.
out.
Okay?
And now with the trie, we can say that N is the length of array one. M is the length of array two.
The result is initially zero. And finally, we want to return this result.
Here in between, we can build an instance of the trie. So, we do this. And now, we can first add the values from array one and then find the longest common prefix. So, we say for every value in array one, trie dot insert. And here, we need to take the current value in string format.
Because remember that we have to go through a ready sheet. So, we transform the number into a string like this in Python.
And now we can move to the next part, which is find the longest common prefix. We say for every value in array two, the result is the maximum between what we already have in the result and try.max_prefix.
And here we take the value in string format.
Okay?
So, this is the entire code.
Let's submit this and see if it works.
As you can see, this works and it's very efficient. This is relatively simple to explain, but might be slightly harder to code. So, in case you didn't get the idea, I highly recommend you to watch this video again from the start. If you found this useful, please drop a like, subscribe, and see you in the next one.
Bye-bye.
Ähnliche Videos
Ubuntu Touch Q&A 190
UBports
241 views•2026-05-17
Learning k8s ep. 3 - The end of the VM
devcentral
102 views•2026-05-15
Iterators and Generators: Real Use Cases
jsmentor-uk
188 views•2026-05-17
TCS NQT Coding Questions Solution (One Shot) | TCS NQT Preparation 2027 | TCS Actual PYQ 2026
knacademy20
2K views•2026-05-17
The 4 Bit AI Training Trick
explaquiz
414 views•2026-05-19
Image to 3D World Workflow 👀
badxstudio
843 views•2026-05-16
Why Learn Algorithms in the AI Era
bitsandproofs
245 views•2026-05-17
NFA - Transition Diagram and Transition Table
nesoacademy
198 views•2026-05-19











