zoukankan      html  css  js  c++  java
  • 288. Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:

    a) it                      --> it    (no abbreviation)
    
         1
    b) d|o|g                   --> d1g
    
                  1    1  1
         1---5----0----5--8
    c) i|nternationalizatio|n  --> i18n
    
                  1
         1---5----0
    d) l|ocalizatio|n          --> l10n
    

    Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no otherword from the dictionary has the same abbreviation.

    Example: 

    Given dictionary = [ "deer", "door", "cake", "card" ]
    
    isUnique("dear") -> false
    isUnique("cart") -> true
    isUnique("cane") -> false
    isUnique("make") -> true

     

    注意 

    dic = [”hello“] word = “hello”  isunique

    public class ValidWordAbbr {
        HashMap<String, String> set;
        public ValidWordAbbr(String[] dictionary) {
            set = new HashMap<String, String>();
            for(int i = 0 ; i < dictionary.length; i++){
                String temp = dictionary[i];
                if(set.containsKey(getKey(temp)) && !set.get(getKey(temp)).equals(temp))
                    set.put(getKey(temp) , "");
                else
                    set.put(getKey(temp) , temp);
                
            }
        }
        
        public String getKey(String s){
            if(s.length() <= 2) return s;
            else{
               return s.charAt(0)+Integer.toString(s.length()-2)+s.charAt(s.length()-1);
            }
        }
    
        public boolean isUnique(String word) {
            if(set.containsKey(getKey(word))){
                return set.get(getKey(word)).equals(word);
            }
            else
                return true;
        }
    }
    
    
    // Your ValidWordAbbr object will be instantiated and called as such:
    // ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
    // vwa.isUnique("Word");
    // vwa.isUnique("anotherWord");
  • 相关阅读:
    CF766 ABCDE
    CF767 C.Garland DFS
    CF767 B. The Queue 贪心+细节
    CF767 A. Snacktower 暴力
    CF760 D Travel Card 简单DP
    CF760 C. Pavel and barbecue 简单DFS
    CF758 D. Ability To Convert 细节处理字符串
    ZOJ 3787 Access System 水
    ZOJ 3785 What day is that day?
    ZOJ 3782 G
  • 原文地址:https://www.cnblogs.com/joannacode/p/6133318.html
Copyright © 2011-2022 走看看