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 prefix; 5 int size = strs.size(); 6 bool flag = true; 7 if (size != 0) { 8 for (int i = 0; i < strs[0].size() && flag; ++i) { 9 for (int j = 1; j < size; ++j) { 10 if (strs[j][i] != strs[0][i]) { 11 flag = false; 12 break; 13 } 14 } 15 if (flag) { 16 prefix += strs[0][i]; 17 } 18 } 19 } 20 return prefix; 21 } 22 };