[C++] Clean Code


  • 1
    class Solution {
    public:
        int findLUSlength(vector<string>& strs) {
            int n = strs.size();
            int maxlen = -1;
            for (int i = 0; i < n; i++) {
                bool uniq = true;
                for (int j = 0; j < n; j++) {
                    if (i != j && isSubsequence(strs[i], strs[j], 0, 0)) {
                        uniq = false;
                        break;
                    }
                }
                if (uniq) {
                    maxlen = max(maxlen, (int)strs[i].length());
                }
            }
    
            return maxlen;
        }
    private:
        bool isSubsequence(string s, string t, size_t si, size_t ti) {
            if (si == s.length()) {
                return true;
            }
            if (ti == t.length()) {
                return false;
            }
    
            return (s[si] == t[ti]) && isSubsequence(s, t, si + 1, ti + 1) || isSubsequence(s, t, si, ti + 1);
        }
    };
    

Log in to reply
 

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