zoukankan      html  css  js  c++  java
  • Leetcode 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 other word 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

    解题思路:

    解题关键点有3个:

    1. 找出word abbreviation 的规律,<first letter><number><last letter>,number = string.length() - 2

    2. 当发现dictionary 里有相同的abbreviation, key 对应的value 变为"" 

    3. The abbreviation of "hello", i.e., h3o already exists in the dictionary.

    Input: ["hello"],isUnique("hello") Output: [false] Expected: [true]

    If the given word itself is in the dictionary, and it has the unique abbreviation, then we should return true.


    Java code:

    public class ValidWordAbbr {
        private Map<String, String> map = new HashMap<String, String>();
    
        public ValidWordAbbr(String[] dictionary) {
            for(int i = 0; i < dictionary.length; i++){
                String key = abbreviate(dictionary[i]);
                if(!map.containsKey(key)){
                    map.put(key, dictionary[i]);
                }else{
                    map.put(key, "");
                }
            }
        }
        
        private String abbreviate(String str){
            return str.charAt(0) + Integer.toString(str.length() - 2)+ str.charAt(str.length()-1);
        }
    
        public boolean isUnique(String word) {
            String x = abbreviate(word);
            if(map.containsKey(x)){
                if(map.get(x).equals(word)){
                    return true;
                }else {
                    return false;
                }
            }
            return true;
        }
    }
    
    // Your ValidWordAbbr object will be instantiated and called as such:
    // ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
    // vwa.isUnique("Word");
    // vwa.isUnique("anotherWord");

    Reference:

    1. https://leetcode.com/discuss/62842/a-simple-java-solution-using-map-string-string

    2. https://leetcode.com/discuss/62824/wrong-test-case

  • 相关阅读:
    Linux记录-批量安装zabbix(转载)
    k8s-基础环境配置(六)
    k8s记录-ntpd时间同步配置(五)
    k8s记录-flanneld+docker网络部署(四)
    Java面试通关要点汇总集
    Java并发编程系列
    码农需要知道的“潜规则”
    领域驱动设计文章
    自动化测试的一些思考
    轻量级爬虫框架
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4869172.html
Copyright © 2011-2022 走看看