Java Solution beats 90%


  • 0
    H
    public class Solution {
        public int findLUSlength(String[] strs) { 
          if(strs == null || strs.length == 0)
          return 0;
          Comparator<String> mycomparator = new Comparator<String>(){
              public int compare(String a,String b){
                  return a.length() - b.length();
              }
          };
          
          Arrays.sort(strs,mycomparator);
          int maxLen = -1;
          for(int i = strs.length-1; i >= 0; i--){
              boolean isUs = true;
              for(int j = strs.length-1; j >= 0; j--){
                  if(strs[j].length() < strs[i].length())
                  break;
                  if(i != j && isSubsequence(strs[i],strs[j])){
                      isUs = false;
                      break;
                  }
              }
              if(isUs)
              maxLen = Math.max(maxLen,strs[i].length());
          }
          return maxLen;
        }
        
        private boolean isSubsequence(String a, String b){
            int start = 0;
            for(int i = 0; i < a.length(); i++){
                int index = b.indexOf(a.charAt(i),start);
                if(index == -1)
                return false;
                else{
                    start = index + 1;
                }
            }
            return true;
        }
    }

Log in to reply
 

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