Short & Clean - Java n^2


  • 0
        public int findLUSlength(String[] strs) {
            int max = Integer.MIN_VALUE; 
            Set<String> set = new HashSet<>(); 
    
            for (int i = 0, j = 0; i < strs.length; i++) {
                String str = strs[i].replace("", ".*"); 
                for (j = 0; j < strs.length; j++)
                    // if the current string is a substring of another string, it's disqualified. 
                    if (i != j && strs[j].matches(str)) break; 
                if (j == strs.length) set.add(strs[i]);
            }
            
            if (set.isEmpty()) return -1; 
            for (String str : set) 
                max = Math.max(max, str.length()); 
                
            return max; 
        }

Log in to reply
 

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