leetcode new problems

This commit is contained in:
2026-02-11 14:48:41 +05:30
parent 192ce44a81
commit 0f52d46c67
29 changed files with 658 additions and 5 deletions
+26
View File
@@ -0,0 +1,26 @@
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;
}
};
+5
View File
@@ -0,0 +1,5 @@
Time complexity : O(n*m) where is n is the number of strings in the list and m is length of the shortest string
space complexity : O(1)
We start from first character of first string and compare it with all other strings, if no mismatch then add it to prefix string else return the prefix string