Skip to content

Commit 426f659

Browse files
authored
Create Best Approach.cpp
1 parent 3087fbb commit 426f659

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string longestCommonPrefix(vector<string>& strs) {
4+
5+
string ans = "";
6+
for(int i = 0; i < strs[0].length(); i++){
7+
char ch = strs[0][i];
8+
9+
bool match = true;
10+
for(int j = 1; j < strs.size(); j++){
11+
if(strs[j].size() < i || ch != strs[j][i]){
12+
match = false;
13+
break;
14+
}
15+
}
16+
17+
if(match == false) break;
18+
else ans.push_back(ch);
19+
}
20+
21+
return ans;
22+
}
23+
};

0 commit comments

Comments
 (0)