LeetCode 14 | LONGEST COMMON PREFIX | STRINGS | C++ [ Approach and Code Explanation]

preview_player
Показать описание
This video contains detailed explanation on #LeetCode problem 14 Longest Common Prefix along with code in C++.

The following question has been asked in various interviews including #Amazon , #Facebook , #Google , #Uber , #Microsoft , #LinkedIn etc.

Рекомендации по теме
Комментарии
Автор

Best explanation on youtube for this question.

vivekgautam
Автор

Thank you so much for the amazing explanation!

janhavik
Автор

Very nice explaination, thanks a lot.

chetanguduru
Автор

hello why you have run loop till string size

yogeshbajaj
Автор

why are we initializing min value as 1000?

simarsinghchugh
Автор

IN PYTHON
s=["flow", "flew", "flower"]
k=s[0]
flag=0
a=""
for x in range(len(k)):
for j in s[1:]:
if k[x]!=j[x]:
flag=1
else:
a=""+k[x]
if flag==1:
break
else:
print(a, end="")

aadhi_dinesh
Автор

class Solution {
public:
string strs) {
if(strs.size() == 1)
return strs[0];
int count = 0;
int mn = INT_MAX;
string str;
for(int i = 0 ; i < strs.size() ; i++){
if(mn > strs[i].length()){
mn = strs[i].length();
str = strs[i];
}
}
for(int i = 0 ; i < mn ; i++){
for(int j = 0 ; j < strs.size() ; j++){
if(strs[j][i] != str[i])
return str.substr(0, count);
}
count++;
}
return "";
}
};



Your input
["ab", "a"]


Output
""


Expected
"a"

shanayaarora
welcome to shbcf.ru