Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 string res; 5 if(strs.empty()) 6 return res; 7 for(int i = 0; i < strs[0].size(); i++) { 8 char ch = strs[0][i]; 9 for(int j = 1; j < strs.size(); j++) { 10 if(strs[j][i] != ch || i == strs[j].size()) { 11 return res; 12 } 13 } 14 res.push_back(ch); 15 } 16 } 17 };