日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

288.Unique Word Abbreviation

發(fā)布時間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 288.Unique Word Abbreviation 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

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 --> d1g1 1 11---5----0----5--8 c) i|nternationalizatio|n --> i18n11---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/

題解:

新題的題目真是越來越長了。 這道題是給定一個數(shù)組Dictionary, 求輸入字符串是否有unique的abbreviation在Dictionary中。我們選擇用Map<String, Set<String>>來做我們存儲數(shù)據(jù)的數(shù)據(jù)結構,然后按照題意做就可以了,還需要判斷一些邊界條件,比如縮寫不在map中直接返回true之類的。 以后一定要牢記,選定了好的數(shù)據(jù)結構,編寫程序就會容易很多。

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");

?

二刷:

跟一刷的方法一樣。就是跟Anagram一樣,用Map<String, Set<String>>來存,使用一個新的方法getAbbr先求出abbr作為key,然后把單詞加入到key的value里。 ?Discuss里面還有很多很好的方法,用map<String, String>之類的,三刷要好好研究。

Java:

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

public class ValidWordAbbr {Map<String, Set<String>> map;public ValidWordAbbr(String[] dictionary) {map = new HashMap<>();for (String s : dictionary) {String abbr = getAbbr(s);if (!map.containsKey(abbr)) {map.put(abbr, new HashSet<String>());}map.get(abbr).add(s);}}public boolean isUnique(String word) {String abbr = getAbbr(word);if (!map.containsKey(abbr) || (map.get(abbr).contains(word) && map.get(abbr).size() == 1)) {return true;}return false;}private String getAbbr(String s) {if (s.length() < 3) {return s;}int len = s.length();return s.substring(0, 1) + (len - 2) + s.substring(len - 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

https://leetcode.com/discuss/71652/java-solution-with-hashmap-string-string-beats-submissions?

轉載于:https://www.cnblogs.com/yrbbest/p/5040715.html

總結

以上是生活随笔為你收集整理的288.Unique Word Abbreviation的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。