1 class Solution {
2 public:
3 string longestCommonPrefix(vector<string>& strs) {
4 int count = strs.size();
5 int prt_ch[300] = {0};
6
7 int max_len = 0;
8 for(int i=0;i<count;i++){
9 max_len = max_len >= strs[i].length()? max_len:strs[i].length();
10 }
11 vector<vector<int>>prt_str;
12 prt_str.resize(max_len);
13 for (int i = 0; i < max_len; i++) {
14 prt_str[i].resize(300);
15 }
16
17 for (int i = 0; i < count; i++){
18 for (int j = 0;j < strs[i].length(); j++){
19 prt_str[j][strs[i][j]]++;
20 }
21 }
22 string ans = "";
23 if (count <= 0)
24 return ans;
25
26 for (int i = 0; i < strs[0].length(); i++){
27 if (prt_str[i][strs[0][i]] == count)
28 ans += strs[0][i];
29 else
30 break;
31 }
32 return ans;
33 }
34 };
35