swift語言,[Swift]LeetCode288. 唯一單詞縮寫 $ Unique Word Abbreviation

 2023-10-18 阅读 19 评论 0

摘要:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公眾號:山青詠芝(shanqingyongzhi)?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)?GitHub地址:https://github.com/s

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10688932.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

swift語言?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

單詞的縮寫詞遵循形式<first letter><number><last letter>。以下是單詞縮寫的一些示例:

a) it                      --> it    (無縮寫)1
b) d|o|g                   --> d1g1    1  11---5----0----5--8
c) i|nternationalizatio|n  --> i18n11---5----0
d) l|ocalizatio|n          --> l10n

縮寫?假設你有一本字典并給了一個單詞,找出它的縮寫在字典中是否唯一。如果字典中沒有其他單詞具有相同的縮寫,則單詞的縮寫是唯一的。

例子:

給定 dictionary = [ "deer", "door", "cake", "card" ]isUnique("dear") -> false
isUnique("cart") -> true
isUnique("cane") -> false
isUnique("make") -> true

Solution:
 1 class Solution {
 2     var m:[String:Set<String>] = [String:Set<String>]()
 3     func ValidWordAbbr(_ dictionary:inout [String]) {
 4         for a in dictionary
 5         {
 6             let arr:[Character] = Array(a)
 7             let k:String = String(arr[0]) + String(a.count - 2) + String(arr.last!)
 8             m[k,default:Set<String>()].insert(a)
 9         }
10     }
11     
12     func isUnique(_ word:String) -> Bool
13     {
14         let arr:[Character] = Array(word)
15         let k:String = String(arr[0]) + String(word.count - 2) + String(arr.last!)
16         let num:Int = m[k,default:Set<String>()].contains(word) ? 1 : 0
17         return num == m[k,default:Set<String>()].count
18     }
19 }

點擊:Playground測試

 1 var sol = Solution()
 2 var arr:[String] = [ "deer", "door", "cake", "card" ]
 3 sol.ValidWordAbbr(&arr)
 4 print(sol.isUnique("dear"))
 5 //Print false
 6 print(sol.isUnique("cart"))
 7 //Print true
 8 print(sol.isUnique("cane"))
 9 //Print false
10 print(sol.isUnique("make"))
11 //Print true

單詞,?

轉載于:https://www.cnblogs.com/strengthen/p/10688932.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/149385.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息