[Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

[Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths,第1张

概述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 lett

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.

Example 1:

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。

示例 1:

输入: 输出: 。["abcw","abcdef"]16 解释: 这两个单词为"abcw","xtfn"

示例 2:

输入: 输出: 这两个单词为 。["a","abcd"]4 解释:"ab","cd"

示例 3:

输入: 输出: ["a","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 = 010         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 res22     }23 }24 25 class ProductHelper {26     let count : Int27     let characters : Int28     init(_ s : String) {29         count = s.count30         let arr = s.unicodeScalars31         var r = 032         for c in arr {33             r |= 1 << Int(c.value - 97) 34         }35         characters = r36     }37 }

804ms

 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 = 015         for i in 0 ..< array.count - 1 {16             for j in 1 ..< array.count {17                 if array[i] & array[j] > 0 {18                     continue19                 }20                 result = max(result,words[i].count * words[j].count)21             }22         }23         return result24     }    25 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths全部内容,希望文章能够帮你解决[Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存