My 12ms AC java solution with two HashSets (98/98)


  • 0
    R
    public int findLUSlength(String[] strs) {
            HashSet<String> hs1 = new HashSet();
            HashSet<String> hs2 = new HashSet();
            int max = -1;
            for(int i=0;i<strs.length;i++)
            {
                if(!hs2.contains(strs[i]))
                {
                    if(!hs1.contains(strs[i])) hs1.add(strs[i]);
                    else
                    {
                        hs1.remove(strs[i]);
                        hs2.add(strs[i]);
                    }
                }
            }
            if(hs1.size()==0) return -1;
            boolean flag = true;
            for(String key1: hs1)
            {
                if(key1.length()<=max) continue;
                for(String key2: hs2)
                {
                    if(key1.length()>key2.length()) continue;
                    if(ismatch(key1,key2))  
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag) max = Math.max(max, key1.length());
                flag = true;
            }
            return max;
        }
        public boolean ismatch(String a, String b)
        {
            int pt1 = 0;
            int pt2 = 0;
            while(pt2<b.length())
            {
                if(a.charAt(pt1)==b.charAt(pt2)) pt1++;
                pt2++;
                if(pt1==a.length()) return true;
            }
            return false;
        }
    

Log in to reply
 

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