Hi, everyone.
So in this video, we are going to solve this question, find the longest common prefix.
So what will be the input?
So input is basically a vector string, OK?
Input is basically a very trusting.
And what we have to do, we have to find the longest common prefix among all the strings.
Let us take some example.
So these are many strings.
String one string doing a string three, we have to find common prefix.
So in this case, what is the common prefix?
So this is the common flix, common prefix.
So my output in this case is basically string A.
OK, let us consider this example.
So in this example, what is the longest common prefix?
So if Al.
So F.L. is basically the longest common prefix, so my output will be F.L..
Let us consider this case so the string one string to end a string three.
So in this case, we do not have any common prefix.
So my output will be basically empty string.
My output will be basically empty string.
Now, in this case, what would be my answer?
So this will be my answer.
OK, durably.
So my output in this case is basically the common prefix is basically GW, so this is very simple problem.
So this is basically an implementation problem.
OK, so what we will do, but we'll also consider this example, so what I will do, I will pick the
first character.
So the first character is, gee, now what will do?
I will iterate over all the string and I will check g g g.
It is fine.
Now go for it.
OK, so e what do you do after finding out.
Geez, fine.
Let's say this is your answer string you will appended to your answer string now e so e present everywhere.
Pick the title character e so easy.
Also present in all the in all of the string.
So you will spend any at all times for this one and for this one.
Now here it is.
So OK, but here I have said OK, so since there is a difference what I will do, I will stop at this
point.
What I will do, I will stop and this will be my answer.
GWC OK, so what we are doing, we are doing character by character matching, ok.
We are doing character by connecting, writing, character matching, this is simply implementation
problem, make the first character now check whether that character is present in all of this string
or not.
Now, the second character, if it is present in all of the string Apprendi, would answer.
Check for the third category of the title character is also present in all the strings, then a bandos
correct in your answer.
Similarly, the fourth character, the fourth character is not listing at this point.
As soon as you find a character that is not present in all of the string, you will stop and you will
return your answer.
And initially your answer will basically be empty string.
Very simple, simple implementation problem.
Now we can write the code, so this basically is a little problem, longest common prefix.
We have to return the string, we have to return the longest common string.
OK, and we are giving crusting as input.
The name is strings.
OK, now let us write the code, so string answer, which is basically initially empty.
OK, this is AMP listing initially.
Now, let us first try to find out the minimum string.
OK, so our function, main element.
So main element will take the beginning of the vector and the end of the vector, so the name of that
is start with strings.
So it will take the beginning and at the end.
OK.
So this minimum element, what it will return, so this will return me, this will return me the minimum
string, OK, meaning testing means basically the string which has the lowest length, OK, lowest size,
minimum size.
So it will it'll be the minimum size of string.
Now what we have to do.
So this example so in this example, what is the medium size of string, so this is geek geeky, OK,
let's call it s so system in size and string.
It is G EKI.
Now, what I will do, I will iterate over this string, I will add it over the string, what they will
do.
I will pick the first character now after picking the first character I read or this Vector and Quddus,
president, president, president, president, president Dan Apprendi here.
OK, Benji.
Now pick the second character E so is present everywhere.
Support E now e so check is present in all the strings, so put E now, so check is not present in all
listing.
This is not present.
So at this point you will stop and you will return on Salvages GW.
OK, simple one.
What we are doing first, this is, this will be Mallalieu Biomet rating over this more or less a string.
Then I'm picking each and every character and Armatrading over this.
All of the strings.
OK, simple one.
Now, what we have to do.
We have to iterate over this string as, OK, the minimum string, so in order to set it so in DayQuil
zero, Ellerston Astarte size.
OK, I am editing out the smallest string now, I am picking the first character, I will pick all the
characters of the string and now I will get rid over this strings.
OK, OK, I will get right over these strings.
So, Jake, zero g listin strings, dot size.
And duplicitous, what do you have to do?
I will compared the character.
OK.
So I will compare the character of the smallest string.
With the Ayyad character, with the character of the Geth string, if they are not equal, that means.
There is no more common prefix and you can anywhere and said, OK.
Now, if the character is present in all resting.
You will apprehend the current Indian substring.
OK, so I said I'd push back.
I character.
Simple and finally, you can read an answer here also, OK, don't answer.
So this is the complete code, OK?
This is the complete cold.
Let us take an example.
So in this case, I am finding out the minimum element.
So what is the minimum element?
Fluid's the minimum element.
OK, so FLW, this is the minimum element and that's what the strings are, basically flower.
And flight.
So what I'm doing, I'm iterating over this floor.
OK, pick the first.
But if no check if it's pleasant.
Yes.
If it's pleasant, yes, then.
So answer is containing, if not the second character, logic, and this president and this president
then so abandoned now, Jack or so or is present, but always not presented at Waterloo.
So this is will the condition of the current character, which is all so always present in this, but
always not present here.
So this condition will become true and I will have done my answer, which is fairly simple.
OK, then this will be used.
So suppose all the strings are same, all the strings at ABC.
All listings at ABC, so in that case, this will never be executed, OK?
This condition will never get executed and it will be my prefix.
My prefix will be ABC only.
So then this condition, then this line will be executed.
OK, simple.
Now, let's try to sum it up called.
OK, so basically, if this is somebody basically if it does not exist.
So what we can do, we can write a condition here.
So basically.
If string darts size is zero.
We can return and protesting, OK, so basically we can get an answer, as says, containing empty string,
or you can also write and protesting here also.
OK.
I guess our code is working fine now if you don't want to find out what you can do.
Suppose you do not.
Want to find out the minimum element so what we can do?
We can take the first string as the minimum element.
OK, so setting aside everything with him, what I will do, I do not want to find out the minimum element
or let's say the minimum element is basically the first string.
OK, first string is basically the smallest string.
So this will be statis.
OK, so the first string is basically the minimum string, OK, then again, Armatrading.
Now, what do you have to do since you have assumed so it will start from one.
OK, Jabel, start from one.
And you are comparing the eight, correct?
OK, so you are comparing the eighth character of the string.
So you have to check first whether they exist or not.
OK, so you have to check first.
So if the eight character is basically.
They're denied access to.
Stringy.
Darkland, you can use Daudt, Lenthall, you can use Dart based, OK, what I've seen, so suppose
I'm using Darcis.
So in that case, also, you will retain your answer.
OK, so what I'm doing here.
So I'm assuming the first string is basically the smallest string.
OK, so this is not this, I am assuming the first dusting is basically the smallest string now I over
the smallest string, but since this is not the smallest thing.
So that's why you have to write this condition, you have to check whether the eighth character exists
or not.
OK, in this case, you do not have to write this condition because the string s was the minimum string
string as well as many strings.
So they are yet corrected.
So they will always exist.
OK, I will always exist.
But here String S is not the smallest string, so you have to check the character exists or not.
Otherwise you will get runtime error at this point.
OK, if the character does not exist.
So this is the only change that we have to do.
OK, so this is the only change.
Now let's meet our cool.
So this is not demeaning string.
OK, so what is their time in the space complexity?
So basically the time complexities, let's say, over time complexities, Eman.
So what is M m is basically the.
Land of the smallest string, OK, and is basically the land of the smallest string and what is and
so and is basically total number of strings.
OK, this is vector nazis' number, total number of strings.
OK, so this is due to M.
OK, this is M letter, the smallest string and the inner loop.
This is the number of string.
So time complexities and.
What is this space complexity?
So if we are considering this space technology space taken by the existing so space is basically big
off M OK, in the worst case, it will be big of him.
OK, so this is the time complexity and this is the space complexity.
So this was just an implementation problem.
We are doing character by character imaging, so if you have any doubt in this question, you can ask
me.
OK, thank you.