[Locked] Unique Word abbreviation

Source: Internet
Author: User

Unique Word abbreviation

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

A) It    --it (no abbreviation)     1b) d|o|g-                   d1g              1    1  1     1---5----0----5--8c) i| Nternationalizatio|n--  i18n              1     1---5----0d) l|ocalizatio|n--          l10n

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

Example:

falsetruefalsetrue

Analysis:

In fact, the topic did not express clearly ... Should contain the following meanings:

1. Dictionary = {"Dear"}, IsUnique ("door") false

2. Dictionary = {"Door", "Door"}, IsUnique ("door")-True

3. Dictionary = {"Dear", "door"}, IsUnique ("door"), false

Therefore, when the abbreviation exists, it is not necessary to return false, if the original dictionary with the query abbreviation consistent with the original string, such as 2 in the Dict two "door", and the query original string "door" consistent, then should also return true.

Code:

classSolution {Private:    stringMAPTOABBR (stringstr) {        stringabbr =""; Abbr+ = str[0]; //If there is only one person, then return directly; there are two, but no number in the middle, two or more, plus the number        if(Str.length () >1) {abbr+ = Str.length () >2? To_string (Str.length ()-2) :""; Abbr+=Str.back (); }        returnabbr; }    //Hashabbr is used to store the abbreviated string, Hashorig is used to store the original stringunordered_multiset<string>Hashabbr, Hashorig;  Public: Solution (Vector<string>dict) {         for(stringstr:dict)            {Hashorig.insert (str);        Hashabbr.insert (MAPTOABBR (str)); }    }    BOOLIsUnique (stringstr) {        stringabbr =maptoabbr (str); //if the abbreviation does not exist in the dictionary, return directly to True        if(Hashabbr.find (abbr) = =hashabbr.end ())return true; //If the abbreviation is in the dictionary, return true if query only corresponds to one of the original strings; otherwise return false        returnHashabbr.count (abbr) = =Hashorig.count (str); }};

[Locked] Unique Word abbreviation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.