class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        // write code here 
        int n = strs.size();
        string s;
        if(n == 0 ) return s;
        if(n == 1) return strs[0];
        s = strs[0];
        int flag = 0;
        for(int i = 1; i < n; i++){
            string temp = strs[i];
            for(int j = 0; j< temp.size() && j < s.size(); j++){
                if(temp[j] == s[j]){
                    flag++;//用flag不断更新flag值
                }
                else  //方便提前退出循环
                  break;
                  
            }
        
          s = s.substr(0, flag );//substr(first, last)函数是截取first下面的元素而不包括last下面的元素                                  //所以不需要写当flag为0时的特殊情况,当flag为0时,本身就是空字符串
            flag = 0;
        }
        return s;

    }
};