Simple Java solution by Stream API


  • 0
    X
    public int findLUSlength(String[] strs) {
        return Arrays.stream(strs).sorted((s1, s2) -> Integer.compare(s2.length(), s1.length()))
                .filter(s -> Arrays.stream(strs).filter(s2 -> s2 != s && s2.length() >= s.length()).noneMatch(s2 -> isSubseq(s, s2))) //
                .findFirst().map(s -> s.length()).orElse(-1);
    }
    
    // Leetcode-392 Is Subsequence: O(M+N), binary search, DP...
    private boolean isSubseq(String s1, String s2) {
        int i = 0, m = s1.length(), n = s2.length();
        for (int j = 0; i < m && j < n; j++) {
            if (s1.charAt(i) == s2.charAt(j)) i++;
        }
        return i == m;
    }
    

Log in to reply
 

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