mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
27 lines
545 B
C++
27 lines
545 B
C++
class Solution {
|
|
public:
|
|
string longestCommonPrefix(vector<string>& strs) {
|
|
if (strs.empty()) return "";
|
|
|
|
string comm = "";
|
|
int n = strs.size();
|
|
|
|
for (int left = 0; left < strs[0].size(); left++) {
|
|
char ch = strs[0][left];
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
if (left >= strs[i].size() || strs[i][left] != ch) {
|
|
return comm;
|
|
}
|
|
}
|
|
|
|
|
|
comm += ch;
|
|
}
|
|
|
|
return comm;
|
|
}
|
|
};
|
|
|