Clean C++ Code


  • 0
    J
    class Solution {
    private:
        bool hasCommon(string a, string b) {
            int i = 0, j = 0;
            int m = a.length(), n = b.length();
            while (i < m && j < n) {
                if (a[i] == b[j]) {
                    i++;
                    j++;
                } else
                    j++;
            }
            if (i == m)
                return true;
            return false;
        }
        
        static bool cmp(string& a, string& b) {
            if (a.length() == b.length())
                return a>b;
            return a.length()>b.length();
        }
    public:
        int findLUSlength(vector<string>& strs) {
            sort(strs.begin(), strs.end(), cmp);
            set<string> duplicate;
            for (int i = 1; i < strs.size(); ++i) {
                if (strs[i] == strs[i - 1]) {
                    duplicate.insert(strs[i]);
                }
            }
            for (int i = 0; i < strs.size(); ++i) {
                if (duplicate.find(strs[i])!=duplicate.end()) {
                    continue;
                }
                bool flag = false;
                for (int j = 0; j < i; ++j) {
                    if (hasCommon(strs[i],strs[j])) {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                    return strs[i].length();
            }
            return -1;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.