Let me explain the question with better examples.


  • 156
    2

    The description (A word's abbreviation is unique if no other word from the dictionary has the same abbreviation) is clear however a bit twisting. It took me a few "Wrong Answer"s to finally understand what it's asking for.
    We are trying to search for a word in a dictionary. If this word (also this word’s abbreviation) is not in the dictionary OR this word and only it’s abbreviation in the dictionary. We call a word’s abbreviation unique.
    EX:

    1) [“dog”]; isUnique(“dig”);   
    

    //False, because “dig” has the same abbreviation with “dog" and “dog” is already in the dictionary. It’s not unique.

    2) [“dog”, “dog"]; isUnique(“dog”);  
    

    //True, because “dog” is the only word that has “d1g” abbreviation.

    3) [“dog”, “dig”]; isUnique(“dog”);   
    

    //False, because if we have more than one word match to the same abbreviation, this abbreviation will never be unique.


  • 0
    C

    Thanks for your explanation! I was so confused about the test case.


  • 5
    V

    I just wonder how many people get AC at first shot for this very 'easy' exercise ?


  • 2
    F

    Thank you for this! The problem seemed easy, but the test cases really confused me.


  • 0
    W

    Thanks for the examples!
    The description above is still not clear for me:
    "this word (also this word’s abbreviation) is not in the dictionary"?
    This word is not in the list and this word's abbreviation does not appear in the list, these are two different things, you cannot just put it into a parenthesis. Pls avoid using parenthesis for mathematical definition.


  • 0
    L

    I wish they'd put these examples there instead. The test cases confused the hell out of me.


  • 0
    N

    @dongning If no other words with the same abbr except itself in the dictionary....


  • 2
    Q

    I didn't subscribe yet, so I don't know, but your example:

    1. [“dog”, “dog"]; isUnique(“dog”); does it imply that the words in dictionary could duplicate? That's really weird.

  • 0

    Share my Java code:

    private Map<String,Set<String>> d;
    public ValidWordAbbr(String[] dictionary) {
        
        d = new HashMap(dictionary.length);
        
        for(int i=0;i<dictionary.length;i++){
            
            String w = dictionary[i];
            String a = getAbbr(w); 
            
            if(d.containsKey(a)){
               Set<String> s = d.get(a);
               if(!s.contains(w)) s.add(w);
            }else{
               Set<String> s = new HashSet();
               s.add(w);
               d.put(a,s);
            }
        }
    }
    
    public boolean isUnique(String word) {
        String a = getAbbr(word);
        if(d.containsKey(a)){
           
           Set<String> s = d.get(a);
           if(s.size() == 1 && s.contains(word)) return true;
           else return false;
        } else return true;
    }
    
    private String getAbbr(String w){
        int sl = w.length() - 2;
        if(sl <= 0) return w;
        return "" + w.charAt(0) + sl + w.charAt(w.length()-1);
    }

  • 9
    O

    To me, the most ridiculous part of the spec is that there may be duplicates in the given dictionary of words.

    Come on, it is called a dictionary!!!


  • 0
    B

    Thanks, this test case is really confusing! Your explanation helps a lot.


  • 0
    A

    This is such a stupid problem.....More like a reading comprehension problem.


Log in to reply
 

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