Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class Solution {
string longestCommonPrefix(vector<string>& strs) {
int len_s = strs.size();
if(len_s == 0) return "";
for(int j = 0; j < strs[0].size(); j++){
char c = strs[0][j];
for(int i = 0; i < len_s; i++){
//Basically we check two cases:
//1 The sub string i reaches its end, which means this should be the common prefix
//2 The current element is not equal to the common element any more
if(j == strs[i].size() || strs[i][j] != c)
return strs[0].substr(0, j);
return strs[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment