★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-axrocttq-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a list of words, we may encode it by writing a reference string S
and a list of indexes A
.git
For example, if the list of words is ["time", "me", "bell"]
, we can write it as S = "time#bell#"
and indexes = [0, 2, 5]
.github
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:spa
Input: words = Output: 10 Explanation: S = ]. ["time", "me", "bell"]"time#bell#" and indexes = [0, 2, 5
Note:code
1 <= words.length <= 2000
.1 <= words[i].length <= 7
.給定一個單詞列表,咱們將這個列表編碼成一個索引字符串 S
與一個索引列表 A
。htm
例如,若是這個列表是 ["time", "me", "bell"]
,咱們就能夠將其表示爲 S = "time#bell#"
和 indexes = [0, 2, 5]
。blog
對於每個索引,咱們能夠經過從字符串 S
中索引的位置開始讀取字符串,直到 "#" 結束,來恢復咱們以前的單詞列表。索引
那麼成功對給定單詞列表進行編碼的最小字符串長度是多少呢?
示例:
輸入: words = 輸出: 10 說明: S = ] 。 ["time", "me", "bell"]"time#bell#" , indexes = [0, 2, 5
提示:
1 <= words.length <= 2000
1 <= words[i].length <= 7
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 }