#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty()) return "";
        sort(strs.begin(), strs.end());
        
        string first = strs[0];
        string last = strs.back();
        string ans = "";
        
        for (int i = 0; i < first.length(); i++) {
            if (i < last.length() && first[i] == last[i]) {
                ans += first[i];
            } else {
                break;
            }
        }
        
        return ans;
    }
};