【python】字符串长度最大乘积

题目链接:https://www.nowcoder.com/practice/480caa5ffd164ac8b71caaa6d0f4e6db

只找到了暴力解法的思路。

s = input().replace('[','').replace(']','').replace('"','').split(',')
def max_product(s):
    if not s:
        return
    a = []
    maxlen = 0
    l = len(s)
    for ss in s:
        a.append(set(ss))
        
    for i in range(l-1):
        for j in range(i+1,l):
            if a[i] & a[j] == set(): #判断集合没有交集,则说明两个字符串无重复字符
                maxlen = max(maxlen,len(a[i])*len(a[j]))
    return maxlen
max_product(s)

 

你可能感兴趣的:(剑指offer)