[LintCode] Word Abbreviation Set

An abbreviation of a word follows the form . 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") // return false
isUnique("cart") // return true
isUnique("cane") // return false
isUnique("make") // return true

Solution 1. A straightforward solution is to simply iterate the given dictionary and compare input word‘s abbreviation with each word‘s abbreviation in the dictionary.

If the the abbreviations are the same and the the words are not the same, return false.  Assume the averge word length is k and there are n words in the dictionary,

the runtime of this solution is O(n*k) for each isUnique operation.

Solution 2.  If the given dictionary does not change and isUnique is called repeatedly, solution 1 of O(n*k) is not efficient as we have to go over the dictionary

every time.  A better approach is to design a class for this isUnique. For a dictionary, it is preprocessed and each abbreviation and its corresponding words are

stored in a hash map.  The key is each unique abbreviation and the value is a hash set of all unique words that share the same abbreviation.

It takes O(n*k) time to construct this hash map. But once it is constructed, it gives us O(k) runtime for checking if isUnique.

Compare to solution 1, this solution uses O(n*k) extra memory. So it is a typical example of time and space tradeoff in algorithm design.

A key note here to remember is that in Java, to iterate a hash set, the Iterator class must be used as highlighted.

 1 public class ValidWordAbbr {
 2     private HashMap<String, HashSet<String>> map;
 3     // @param dictionary a list of word
 4     public ValidWordAbbr(String[] dictionary) {
 5         map = new HashMap<String, HashSet<String>>();
 6         for(String s : dictionary){
 7             String abbrKey = convertToAbbr(s);
 8             if(map.containsKey(abbrKey)){
 9                 map.get(abbrKey).add(s);
10             }
11             else{
12                 HashSet<String> set = new HashSet<String>();
13                 set.add(s);
14                 map.put(abbrKey, set);
15             }
16         }
17     }
18     /**
19      * @param word a string
20      * @return true if its abbreviation is unique or false
21      */
22     public boolean isUnique(String word) {
23         String abbrKey = convertToAbbr(word);
24         if(!map.containsKey(abbrKey)){
25             return true;
26         }
27         else if(map.get(abbrKey).size() == 1){
28             Iterator<String> iter = map.get(abbrKey).iterator();
29             return word.equals(iter.next());
30         }
31         return false;
32     }
33
34     private String convertToAbbr(String word){
35         if(word.length() <= 2){
36             return word;
37         }
38         return String.valueOf(word.charAt(0)) + String.valueOf(word.length() - 2)
39                 + String.valueOf(word.charAt(word.length() - 1));
40     }
41 }
42
43 /**
44  * Your ValidWordAbbr object will be instantiated and called as such:
45  * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
46  * boolean param = obj.isUnique(word);
47  */

Related Problems

Two Sum - Data Structure Design

时间: 2024-12-12 20:10:27

[LintCode] Word Abbreviation Set的相关文章

[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", &

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

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

408. 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", &

411. Minimum Unique Word Abbreviation

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

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