zoukankan      html  css  js  c++  java
  • [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(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                   --> 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") -> false
    isUnique("cart") -> true
    isUnique("cane") -> false
    isUnique("make") -> true

    单词的缩写词遵循形式<first letter><number><last letter>。以下是单词缩写的一些示例:

    a) it                      --> it    (无缩写)
    
         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
    

    假设你有一本字典并给了一个单词,找出它的缩写在字典中是否唯一。如果字典中没有其他单词具有相同的缩写,则单词的缩写是唯一的。

    例子:

    给定 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
  • 相关阅读:
    HBase 高性能加入数据
    Please do not register multiple Pages in undefined.js 小程序报错的几种解决方案
    小程序跳转时传多个参数及获取
    vue项目 调用百度地图 BMap is not defined
    vue生命周期小笔记
    解决小程序背景图片在真机上不能查看的问题
    vue项目 菜单侧边栏随着右侧内容盒子的高度实时变化
    vue项目 一行js代码搞定点击图片放大缩小
    微信小程序进行地图导航使用地图功能
    小程序报错Do not have xx handler in current page的解决方法
  • 原文地址:https://www.cnblogs.com/strengthen/p/10688932.html
Copyright © 2011-2022 走看看