[Swift]LeetCode318. 最大單詞長度乘積 | Maximum Product of Word Lengths

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-awvsuuez-km.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.git

Example 1:github

Input: 
Output: The two words can be .["abcw","baz","foo","bar","xtfn","abcdef"]16 
Explanation:"abcw", "xtfn"

Example 2:數組

Input: 
Output: The two words can be .["a","ab","abc","d","cd","bcd","abcd"]4 
Explanation:"ab", "cd"

Example 3:微信

Input: 
Output: No such pair of words.["a","aa","aaa","aaaa"]0 
Explanation:

給定一個字符串數組 words,找到 length(word[i]) * length(word[j]) 的最大值,而且這兩個單詞不含有公共字母。你能夠認爲每一個單詞只包含小寫字母。若是不存在這樣的兩個單詞,返回 0。app

示例 1:spa

輸入: 
輸出: 。["abcw","baz","foo","bar","xtfn","abcdef"]16 
解釋: 這兩個單詞爲"abcw", "xtfn"

示例 2:code

輸入: 
輸出: 這兩個單詞爲 。["a","ab","abc","d","cd","bcd","abcd"]4 
解釋:"ab", "cd"

示例 3:htm

輸入: 
輸出: ["a","aa","aaa","aaaa"]0 
解釋: 不存在這樣的兩個單詞。

332 ms
 1 class Solution {    
 2     func maxProduct(_ words: [String]) -> Int {
 3         if words.isEmpty {
 4             return 0
 5         }
 6         
 7         let products = words.map{ProductHelper($0)}
 8         
 9         var res = 0
10         
11         for i in 0..<products.count {
12             for j in i+1..<products.count {
13                 let p1 = products[i]
14                 let p2 = products[j]
15                 if p1.characters & p2.characters == 0 {
16                     res = max(p1.count * p2.count, res)
17                 }
18             }
19         }
20         
21         return res
22     }
23 }
24 
25 class ProductHelper {
26     let count : Int
27     let characters : Int
28     init(_ s : String) {
29         count = s.count
30         let arr = s.unicodeScalars
31         var r = 0
32         for c in arr {
33             r |= 1 << Int(c.value - 97) 
34         }
35         characters = r
36     }
37 }

804msblog

 1 class Solution {
 2     func maxProduct(_ words: [String]) -> Int {
 3         let aValue = "a".unicodeScalars.first!.value
 4         if words.count <= 1 { return 0 }
 5         var array = [Int]()
 6         for word in words {
 7             var a = 0
 8             for c in word.unicodeScalars {
 9                 a = a | (1 << (c.value - aValue))
10             }
11             array.append(a)
12         }
13         
14         var result = 0
15         for i in 0 ..< array.count - 1 {
16             for j in 1 ..< array.count {
17                 if array[i] & array[j] > 0 {
18                     continue
19                 }
20                 result = max(result, words[i].count * words[j].count)
21             }
22         }
23         return result
24     }    
25 }
相關文章
相關標籤/搜索