Not understanding the last test case ["a"] ["a", "a"]


  • 0
    A

    I'm not getting the last test case with following inputs:

    ["a"]
    ["a","a"],

    My code is returning false and true is expected.
    Any idea?
    Below code works for 52/53 test cases.

    """

    private Map<String, Integer> uniqueWords;
    private Set<String> dict;
    
    public ValidWordAbbr(String[] dictionary) {
        dict = new HashSet<>(Arrays.asList(dictionary));
        uniqueWords = new HashMap<>();
        for (String s : dictionary) {
            String abr = getAbbr(s);
            uniqueWords.put(abr, uniqueWords.getOrDefault(abr, 0) + 1);
        }
        
    }
    
    public boolean isUnique(String word) {
        String abr = getAbbr(word);
        if (dict.contains(word) && uniqueWords.get(abr) == 1) return true;
        else return !(uniqueWords.containsKey(abr));
    }
    
    public String getAbbr(String word) {
        int n = word.length();
        if (n <= 2)
    	return word;
        return word.charAt(0) + Integer.toString(n - 2) + word.charAt(n - 1);
    }
    

    """


Log in to reply
 

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