Skip to content

Commit 9b76e40

Browse files
authored
Create 14_Longest Common Prefix
1 parent 0bb14db commit 9b76e40

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: 14_Longest Common Prefix

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
class Solution {
3+
public:
4+
string longestCommonPrefix(vector<string>& strs) {
5+
6+
while (strs.size()==0) return "";
7+
8+
string prefix = strs[0];
9+
10+
for(int i = 1; i < strs.size(); i++){
11+
while(strs[i].substr(0,prefix.size()) != prefix){
12+
prefix = prefix.substr(0, prefix.size() - 1);
13+
}
14+
15+
}
16+
return prefix;
17+
18+
}
19+
};

0 commit comments

Comments
 (0)