9 ms - fastest:


  • -1
    R

    Whoever wrote this code, has checked for each and every test case .......
    Just happened to see the fastest code. Couldn't help but share.
    WOW

    class Solution {
        public int minStickers(String[] stickers, String target) {
            int len = stickers.length;
    
            if("quietchord".equals(target)|| "simpleexample".equals(target)) {
                return -1;
            }
            if("asact".equals(target)|| "sittook".equals(target) || "paintcan".equals(target)|| "earthbad".equals(target)|| "allrow".equals(target)|| "barthose".equals(target)) {
                return 2;
            }
            if("caselight".equals(target)) {
                return 6;
            }
            if("easesoldier".equals(target)||"materialhalf".equals(target) ||"visitcover".equals(target) || "mustduring".equals(target) || "knewdiscuss".equals(target) || "spendneck".equals(target) || "differfoot".equals(target)||"distantcollect".equals(target) || "pluralmark".equals(target)||"springmuch".equals(target) || "drydry".equals(target) || "wavecapital".equals(target) ||"bringmethod".equals(target)||"eventfat".equals(target) || "yesshore".equals(target)||"operateform".equals(target) || "likehuman".equals(target) || "overfresh".equals(target) || "ranwind".equals(target) ||"truelove".equals(target) ||"travelbell".equals(target) || "togetherhand".equals(target)||"towardkept".equals(target)||"liftwife".equals(target)|| "silentran".equals(target)|| "weresurprise".equals(target) ||"womencry".equals(target) ||"lessremember".equals(target)|| target.equals("solveside") || "firstthick".equals(target)||"paychief".equals(target) || "grayenemy".equals(target) || "feltmillion".equals(target)||"decimalanimal".equals(target)||"donepower".equals(target)||"thosecontinent".equals(target)) {
                return 4;
            }
            if("catread".equals(target) ||  "toport".equals(target) || "originalchair".equals(target) || "mighthave".equals(target)) {
                return 3;
            }
            if("sharpcenter".equals(target) ||"drinkinvent".equals(target) || "middlebuy".equals(target) ||  "separatewhich".equals(target) || "lakeblue".equals(target)) {
                return 5;
            }
            if("gasproper".equals(target) || "camereal".equals(target)|| "deathsing".equals(target)|| "stoodcrease".equals(target)) {
                return 3;
            }
            if("putcat".equals(target) || "dealplan".equals(target)|| "uplog".equals(target)|| "ideacost".equals(target) || "dresspost".equals(target)||"governgreat".equals(target))  {
                return 3;
            }
            if("togethernear".equals(target) || "placeclock".equals(target) || "supportclose".equals(target)) {
                return 5;
            }
            if(target.equals("greatscore") || "appearreason".equals(target)) {
                return 7;
            }
            if(target.equals("centorder") && stickers.length == 5) {
                return 4;
            }
            if(len >= 5 && target.equals("lastwest")) {
                return 4;
            }
            if(len >= 6) {
                return 3;
            }
            Set<Character> sSet = new HashSet<>();
            for(String sticker : stickers) {
                for(char c : sticker.toCharArray()) {
                    sSet.add(c);
                }
            }
            Set<Character> tSet = new HashSet<>();
            for(char c : target.toCharArray()) {
                if(! sSet.contains(c)) {
                    return -1;
                }
                tSet.add(c);
            }
    
            System.out.println(stickers.length);
    
            return 3;
        }
    }
    

Log in to reply
 

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