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 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

链接: http://leetcode.com/problems/unique-word-abbreviation/

题解:

新题的题目真是越来越长了。 这道题是给定一个数组Dictionary, 求输入字符串是否有unique的abbreviation在Dictionary中。我们选择用Map<String, Set<String>>来做我们存储数据的数据结构,然后按照题意做就可以了,还需要判断一些边界条件,比如缩写不在map中直接返回true之类的。 以后一定要牢记,选定了好的数据结构,编写程序就会容易很多。

Time Complexity - O(n * L), Space Complexity - O(n * L)。

public class ValidWordAbbr {
    private Map<String, HashSet<String>> map;

    public ValidWordAbbr(String[] dictionary) {
        this.map = new HashMap<>();
        for(int i = 0; i < dictionary.length; i++) {
            String abbr = getAbbr(dictionary[i]);
            if(!map.containsKey(abbr)) {
                HashSet<String> set = new HashSet<>();
                set.add(dictionary[i]);
                map.put(abbr, set);
            } else {
                if(!map.get(abbr).contains(dictionary[i])) {
                    map.get(abbr).add(dictionary[i]);
                }
            }
        }
    }

    public boolean isUnique(String word) {
        if(map.size() == 0 || word.length() < 3) {
            return true;
        }
        String abbr = getAbbr(word);
        if(!map.containsKey(abbr) || (map.get(abbr).contains(word) && map.get(abbr).size() == 1)) {
            return true;
        } else {
            return false;
        }
    }

    private String getAbbr(String s) {
        if(s.length() < 3) {
            return s;
        } else {
            return s.substring(0, 1) + String.valueOf(s.length() - 2) + s.substring(s.length() - 1);
        }
    }
}

// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");

Reference:

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

https://leetcode.com/discuss/61658/share-my-java-solution

时间: 2024-10-18 06:42:11

288. Unique Word Abbreviation的相关文章

[LeetCode] 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 --&

[Locked] Unique Word Abbreviation

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

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 --&

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 --&

Unique Word Abbreviation -- LeetCode

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 --&

411. Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "

Leetcode: Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &