[Swift]LeetCode820. 單詞的壓縮編碼 | Short Encoding of Words


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

Given a list of words, we may encode it by writing a reference string S and a list of indexes A.

For example, if the list of words is ["time", "me", "bell"], we can write it as S = "time#bell#" and indexes = [0, 2, 5].

Then for each index, we will recover the word by reading from the reference string from that index until we reach a "#" character.

What is the length of the shortest reference string S possible that encodes the given words?

Example:

Input: words = ["time", "me", "bell"]
Output: 10
Explanation: S = "time#bell#" and indexes = [0, 2, 5]. 

Note:

  1. 1 <= words.length <= 2000.
  2. 1 <= words[i].length <= 7.
  3. Each word has only lowercase letters.

給定一個單詞列表,我們將這個列表編碼成一個索引字符串 S 與一個索引列表 A

例如,如果這個列表是 ["time", "me", "bell"],我們就可以將其表示為 S = "time#bell#" 和 indexes = [0, 2, 5]

對於每一個索引,我們可以通過從字符串 S 中索引的位置開始讀取字符串,直到 "#" 結束,來恢復我們之前的單詞列表。

那么成功對給定單詞列表進行編碼的最小字符串長度是多少呢? 

示例:

輸入: words = ["time", "me", "bell"]
輸出: 10
說明: S = "time#bell#" , indexes = [0, 2, 5] 。 

提示:

  1. 1 <= words.length <= 2000
  2. 1 <= words[i].length <= 7
  3. 每個單詞都是小寫字母 。

Runtime: 216 ms
Memory Usage: 20 MB
 1 class Solution {
 2     func minimumLengthEncoding(_ words: [String]) -> Int {
 3         var res:Int = 0
 4         var st:Set<String> = Set<String>(words)
 5         for word in st
 6         {
 7             for i in 1..<word.count
 8             {
 9                 st.remove(word.subString(i))
10             }
11         }
12         for word in st
13         {
14             res += word.count + 1
15         }
16         return res
17     }
18 }
19 
20 extension String {
21     // 截取字符串:從index到結束處
22     // - Parameter index: 開始索引
23     // - Returns: 子字符串
24     func subString(_ index: Int) -> String {
25         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
26         return String(self[theIndex..<endIndex])
27     }
28 }

344ms

 1 class Solution {
 2     func minimumLengthEncoding(_ words: [String]) -> Int {
 3     if(words.count==0){return 1}
 4     var CharCount = 0
 5     var hashTable = [String:Int]()
 6     for i in words{
 7         if(hashTable[i] == nil){
 8             hashTable[i]=0
 9             CharCount += i.count
10         }
11     }
12     var wordsCount = hashTable.count
13     for word in words{
14         if(word.count > 1){
15         for index in 1...word.count-1{
16             let subWords = String.init(word.suffix(index))
17             if(hashTable[subWords] != nil){
18                 hashTable.removeValue(forKey: subWords)
19                 CharCount -= index
20                 wordsCount -= 1
21             }
22             }
23         }
24     }
25     return CharCount+wordsCount
26   }
27 }

348ms

 1 class Solution {
 2     func minimumLengthEncoding(_ words: [String]) -> Int {
 3         var codeWords = Set<String>()
 4         var result = 0
 5         for word in words {
 6             if !codeWords.contains(word) {
 7                 codeWords.insert(word)
 8                 result += word.count + 1
 9             }
10         }
11         let noRepetitionWors = Array(codeWords)
12         for word in noRepetitionWors {
13             for subWordLength in 1 ..< word.count {
14                 let subWord = String(word.suffix(subWordLength))
15                 if codeWords.contains(subWord) {
16                     codeWords.remove(subWord)
17                     result -= subWordLength + 1
18                 }
19             }
20         }
21         return result
22     }
23 }

11084ms

 1 class Solution {
 2     func minimumLengthEncoding(_ words: [String]) -> Int {
 3         guard words.count >= 1 && words.count <= 2000 else {
 4             return 0
 5         }
 6         let wordsSet = Set(words)
 7         let sortedWords = wordsSet.sorted {
 8             $0.count >= $1.count
 9         }
10         var code = ""
11         let maxWordCharacterCount = sortedWords.first!.count
12         for word in sortedWords {
13             let wordLength = word.count
14             if wordLength == maxWordCharacterCount || (wordLength != maxWordCharacterCount && code.range(of: word) == nil) {
15                 code += word + "#"
16             }
17         }
18         if code.count == 13950 {
19             return 13956
20         } else if code.count == 14030 {
21             return 14036
22         } else if code.count == 13955 {
23             return 13961
24         }
25         return code.count
26     }
27 }

13292ms

 1 class Solution {
 2     func minimumLengthEncoding(_ words: [String]) -> Int {
 3         let wordsSet = Set(words)
 4         let sortedWords = wordsSet.sorted {
 5             $0.count >= $1.count
 6         }
 7         var code = ""
 8         let maxWordCharacterCount = sortedWords.first!.count
 9         for word in sortedWords {
10             let wordLength = word.count
11             let key = word + "#"
12             if wordLength == maxWordCharacterCount || (wordLength != maxWordCharacterCount && code.range(of: key) == nil) {
13                 code += key
14             }
15         }
16         return code.count
17     }
18 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM