[Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words

[Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words,第1张

概述International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on. For co

International Morse Code defines a standard enCoding where each letter is mapped to a serIEs of dots and dashes,as follows: "a" maps to ".-""b" maps to "-...""c" maps to "-.-.",and so on.

For convenIEnce,the full table for the 26 letters of the English Alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now,given a List of words,each word can be written as a concatenation of the Morse code of each letter. For example,"cba" can be written as "-.-..--...",(which is the concatenation "-.-." + "-..." + ".-"). We‘ll call such a concatenation,the transformation of a word.

Return the number of different transformations among all words we have.

Example:input: words = ["gin","Zen","gig","msg"]Output: 2Explanation: The transformation of each word is:"gin" -> "--...-.""Zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."There are 2 different transformations,"--...-." and "--...--.".

Note:

The length of words will be at most 100. Each words[i] will have length in range [1,12]. words[i] will only consist of lowercase letters.

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-""b" 对应 "-...""c" 对应 "-.-.",等等。

为了方便,所有26个英文字母对应摩尔斯密码表如下:

[".-","--.."]

给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。

返回我们可以获得所有词不同单词翻译的数量。

例如:输入: words = ["gin","msg"]输出: 2解释: 各单词翻译如下:"gin" -> "--...-.""Zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."共有 2 种不同翻译,"--...-." 和 "--...--.". 

注意:

单词列表words 的长度不会超过 100。 每个单词 words[i]的长度范围为 [1,12]。 每个单词 words[i]只包含小写字母。 Runtime: 16 ms Memory Usage: 20.2 MB
 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3         let array = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 4         return Set<String>(words.map { (word) -> String in 5             let aWord = word.uppercased() 6             var result = "" 7             aWord.unicodeScalars.forEach({ (s) in 8                 result += array[Int(s.value) - 65] 9             })10             return result11         }).count12     }13 }
Runtime: 16 ms Memory Usage: 19.9 MB
 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3         var morse:[String] = [".-","--.."] 4         var s:Set<String> = Set<String>() 5         for word in words 6         { 7             var t:String = String() 8             for c in word 9             {10                 t += morse[c.ascii - 97]11             }12             s.insert(t)13         }14         return s.count15     }16 }17 18 //Character扩展 19 extension Character  20 {  21   //Character转ASCII整数值(定义小写为整数值)22    var ascii: Int {23        get {24            return Int(self.unicodeScalars.first?.value ?? 0)25        }       26     }27 }

24ms

 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3         func transform(_ position: UInt32) -> String? { 4             let morse = [".-","--.."] 5             let offset = "a".unicodeScalars.first!.value 6             return morse[Int(position - offset)] 7         } 8         var result = Set<String>() 9         for word in words {10             let morseCode = word.unicodeScalars.compactMap { transform($0.value) }.joined()11             result.update(with: morseCode)12         }13         return result.count14     }15 }

24ms

 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3         var morse : [String] = Array(repeating: "",count: 97) 4         morse.append(contentsOf: [".-","--.."])         5          6         var wordsSet : Set<String> = [] 7         var morseWord : String = "" 8         for word in words { 9             morseWord = ""10             for w in word.unicodeScalars {11                 morseWord.append(contentsOf: morse[Int(w.value)])12             }13             wordsSet.update(with: morseWord)14         }15         return wordsSet.count16     }17 }

 

40ms

 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3  4         let morseCodeArray = [".-","--.."] 5         let Alphabets = "abcdefghijklmnopqrstuvwxyz" 6          7         var morseSet: Set<String> = [] 8          9         for word in words {10             var morseWord: String = ""11             12             for character in word {13                 14                 var order = 015                 var counter = 016                 17                 for Alphabet in Alphabets {18                     19                     if character == Alphabet {20                         counter = order21                     }22                     order += 123                 }24                 morseWord += morseCodeArray[counter]25             }26             morseSet.insert(morseWord)27         }28         return morseSet.count29     }30 }

36ms

 1 class Solution { 2         private let passwordtable: [Character : String] = [ 3         "a" : ".-", 4         "b" : "-...", 5         "c" : "-.-.", 6         "d" : "-..", 7         "e" : ".", 8         "f" : "..-.", 9         "g" : "--.",10         "h" : "....",11         "i" : "..",12         "j" : ".---",13         "k" : "-.-",14         "l" : ".-..",15         "m" : "--",16         "n" : "-.",17         "o" : "---",18         "p" : ".--.",19         "q" : "--.-",20         "r" : ".-.",21         "s" : "...",22         "t" : "-",23         "u" : "..-",24         "v" : "...-",25         "w" : ".--",26         "x" : "-..-",27         "y" : "-.--",28         "z" : "--..",29         ]30     31     func uniqueMorseRepresentations(_ words: [String]) -> Int {32         return Set(words.map {33             $0.map({34                 passwordtable[$0]!35             }).reduce("",{36                 $0 + $137             })38         }).count39     }40 }

40ms

 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int { 3         // 字母表莫尔斯密码字典 4         var Alphabet = Dictionary<String,String> ()  5         let charary = Array("abcdefghijklmnopqrstuvwxyz") 6         let morseAry = [".-","--.."] 7         var res = Array<String>() 8         var resList = Array<String>() 9         10         for (index,value) in charary.enumerated() {11             // String(value):char转string 映射字母表莫尔斯密码成字典12             Alphabet[String(value)] = morseAry[index] 13         }14         // 遍历 ["gin","msg"]15         for (_,value) in words.enumerated() { 16             var tmp = ""17             // 遍历 "gin"18             for (_,value1) in String(value).enumerated() { 19                 tmp.append(Alphabet[String(value1)]!)20             }21             res.append(tmp) // "gin" => "--...-."22         }23         // 去重24         for (_,value) in res.enumerated() { 25             if !resList.contains(value) {26                 resList.append(value)27             }28         }29         30         return resList.count31     }32 }

48ms

 1 class Solution { 2     func uniqueMorseRepresentations(_ words: [String]) -> Int {         3         guard words.count > 1 else { 4             return words.count 5         } 6  7         var transformationMap:[String: Bool] = [String: Bool]() 8         var moorseDict:[Character: String] =  9             ["a": ".-","b": "-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."]10         11 12         for var word in words {13             var moorseCode = ""14             for var letter in word {15                 if let moorse = moorseDict[letter] {16                     moorseCode += moorse17                 }18             }19             transformationMap[moorseCode] = true20         }21         22         return transformationMap.keys.count23     }24 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words全部内容,希望文章能够帮你解决[Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018731.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存