filmov
tv
14. Longest Common Prefix | LeetCode Easy | Python Solution | String, Array

Показать описание
Leetcode easy problem 14. Longest Common Prefix, detailed explanation and solution in python language.
#leetcode #python #solution
Problem Statement:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
______________________________________________________________
LeetCode problem solving helps in improving one's problem solving and coding skills . Also, it helps in clearing technical interviews at top tech companies like Microsoft, Google, Amazon, Facebook, Walmart, Apple etc.
(FAANGM, MAANGM, Product based companies)
CC of video:
we are given an array of strings, and we need to return the longest common prefix.
i.e., we need to check whether there are any prefix common to all the strings of the given array, and return it.
for example, in this case, we can see that the prefix 'fl' is common to all the strings.
and in case, if there are no common prefix, we need to return an empty string.
for example, in this case, we can see that there are no prefix common to all the strings.
to solve this problem, what we can do is, we can take the first string as a reference and compare it with every other strings in the array character by character.
lets take an example.
here, we will take the first string 'busy' as a reference.
so we will be iterating till we reach the end of this particular string.
during each iteration, we will check whether the character at the current index are same for all the strings of the array.
here at index 0, the character is 'b'. we will check whether the character at index 0 of all the remaining strings are 'b'.
since the character is same for all the strings, we will include it in the result.
at index 1, we have the character 'u'.
since the character at index 1 is same for all the strings, we will include it in the result.
at index 2, we have the character 's'.
since the character at index 2 is same for all the strings, we will include it in the result.
now here at index 3, we have character 'y' in this string.
but for the second string, the character at index 3 is 'i'.
that means the common prefix cannot be continued further and therefore we can terminate the iteration here.
so the longest common prefix for the given array of string is 'bus'.
also, one more thing we need to consider is end of a string.
for example, here in this case, since the third string has already ended, we can terminate the iteration here.
now lets take another example.
here, at index 0 itself, the character is different for the third string.
so the result will be an empty string.
now, lets code the solution.
we will store the length of the given array in this variable.
if there is only one string in the given array, we need not have to check anything, we can simply return that one string.
for storing the common prefix characters, we will have an empty array.
and at last, we will be returning it as a string.
now lets iterate through the first string of the given array, character by character.
for each index, we need to check through the remaining strings in the given array.
if characters are different, or if any string has ended, we cannot have a common prefix from this point, and therefore we can terminate the check there itself.
and since, we do have an outer loop, we need to inform the outer loop that we need to terminate the check.
for that, we will be using this variable.
and if all characters at the current index were same, we will add it to the result.
#leetcode #python #solution
Problem Statement:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
______________________________________________________________
LeetCode problem solving helps in improving one's problem solving and coding skills . Also, it helps in clearing technical interviews at top tech companies like Microsoft, Google, Amazon, Facebook, Walmart, Apple etc.
(FAANGM, MAANGM, Product based companies)
CC of video:
we are given an array of strings, and we need to return the longest common prefix.
i.e., we need to check whether there are any prefix common to all the strings of the given array, and return it.
for example, in this case, we can see that the prefix 'fl' is common to all the strings.
and in case, if there are no common prefix, we need to return an empty string.
for example, in this case, we can see that there are no prefix common to all the strings.
to solve this problem, what we can do is, we can take the first string as a reference and compare it with every other strings in the array character by character.
lets take an example.
here, we will take the first string 'busy' as a reference.
so we will be iterating till we reach the end of this particular string.
during each iteration, we will check whether the character at the current index are same for all the strings of the array.
here at index 0, the character is 'b'. we will check whether the character at index 0 of all the remaining strings are 'b'.
since the character is same for all the strings, we will include it in the result.
at index 1, we have the character 'u'.
since the character at index 1 is same for all the strings, we will include it in the result.
at index 2, we have the character 's'.
since the character at index 2 is same for all the strings, we will include it in the result.
now here at index 3, we have character 'y' in this string.
but for the second string, the character at index 3 is 'i'.
that means the common prefix cannot be continued further and therefore we can terminate the iteration here.
so the longest common prefix for the given array of string is 'bus'.
also, one more thing we need to consider is end of a string.
for example, here in this case, since the third string has already ended, we can terminate the iteration here.
now lets take another example.
here, at index 0 itself, the character is different for the third string.
so the result will be an empty string.
now, lets code the solution.
we will store the length of the given array in this variable.
if there is only one string in the given array, we need not have to check anything, we can simply return that one string.
for storing the common prefix characters, we will have an empty array.
and at last, we will be returning it as a string.
now lets iterate through the first string of the given array, character by character.
for each index, we need to check through the remaining strings in the given array.
if characters are different, or if any string has ended, we cannot have a common prefix from this point, and therefore we can terminate the check there itself.
and since, we do have an outer loop, we need to inform the outer loop that we need to terminate the check.
for that, we will be using this variable.
and if all characters at the current index were same, we will add it to the result.
Комментарии