Longest Common Prefix | Leetcode Top Interview 150

preview_player
Показать описание
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies.
About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer.

Problem Description - 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 "".
Рекомендации по теме
Комментарии
Автор

Solution code - class Solution {
public:
string strs) {
string ans;
int n = strs.size();
int flag = 1;
for (int j=0;j<strs[0].length();j++) {
flag = 1;
for (int i=1;i<n;i++) {
if (strs[i][j] != strs[0][j]) {
flag = 0;
break;
}
}
if (flag) {
ans += strs[0][j];
} else {
break;
}
}
return ans;

}
};

CodingWithAnurag-