程式語言 - LeetCode - C++ - 522. Longest Uncommon Subsequence II



題目:


解答:

class Solution {
public:
    int findLUSlength(vector<string>& strs) {
        int ans = -1;

        auto is_seq = [&](string a, string b) {
            int i = 0;
            int j = 0;

            while (i < a.size() && j < b.size()) {
                if (a[i] == b[j]) {
                    i += 1;
                }

                j += 1;
            }

            return i == a.size();
        };

        for (int i = 0; i < strs.size(); ++i) {
            bool uncomm = true;

            for (int j = 0; j < strs.size(); ++j) {
                if (i == j) {
                    continue;
                }

                if (is_seq(strs[i], strs[j])) {
                    uncomm = false;
                    break;
                }
            }

            if (uncomm) {
                ans = max(ans, static_cast<int>(strs[i].size()));
            } 
        }

        return ans;
    }
};