博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Unique Word Abbreviation
阅读量:6902 次
发布时间:2019-06-27

本文共 1694 字,大约阅读时间需要 5 分钟。

Problem Description:

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)     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 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") -> falseisUnique("cart") -> trueisUnique("cane") -> falseisUnique("make") -> true 

To check for unique abbreviation, we maintain a mapping from a specific abbreviation to all words which have the abbreviation. Then we just need to check no other words have the same abbreviation as the given word.

The code is as follows.

class ValidWordAbbr {public:    ValidWordAbbr(vector
&dictionary) { for (string& d : dictionary) { int n = d.length(); string abbr = d[0] + to_string(n) + d[n - 1]; mp[abbr].insert(d); } } bool isUnique(string word) { int n = word.length(); string abbr = word[0] + to_string(n) + word[n - 1]; return mp[abbr].count(word) == mp[abbr].size(); }private: unordered_map
> mp;};// Your ValidWordAbbr object will be instantiated and called as such:// ValidWordAbbr vwa(dictionary);// vwa.isUnique("hello");// vwa.isUnique("anotherWord");

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4851799.html

你可能感兴趣的文章
性能监测工具 dstat
查看>>
匿名无须交互输入用户名和密码的samba配置方法(security=user)
查看>>
我的友情链接
查看>>
UTM四公子
查看>>
6016.Cacti监控思科6509交换机告警灯和系统运行状态
查看>>
sed-n/N/g/G/h/H/x…
查看>>
供应链管理-初见
查看>>
用 JavaScript 操作字符串
查看>>
Oracle计算时间差常用函数
查看>>
外链的理解
查看>>
机器学习:选对时机直线超车
查看>>
Java基础基本常识
查看>>
谈谈Python实战数据可视化之matplotlib模块(实战篇)
查看>>
2.27linux和windows互传文件 3.1 用户配置文件和密码配置文件 3.2 用户组管理
查看>>
Java程序员需要技术能力达到什么程度,才能拿到月薪30k?
查看>>
Java之品优购课程讲义_day14(5)
查看>>
Jenkins 持续集成使用教程
查看>>
MongoDB复制集
查看>>
oracle sql
查看>>
强制弹出页面代码(以及自动最小化功能)
查看>>