两子数组交集问题

同事刷题:
给定一个元素,类型为小写字符串数组, 请计算两个没有相同字符的子数组的长度乘积的最大值, 若果没有符合条件的子数组, 返回0.

输入条件: 输入为半角逗号分隔的小写字符串数组, 数组长度[2,100], 字符串长度[0,50]

#!/usr/bin/env python3
import re

arr=[]
arr = input('Enter the str: ')

# get between [2,100] num of lowcase chars splittd with','
while 1:
    if ((2<=len(arr)<=100) and not re.match(r'.*[^a-z,]+', arr)):
        if (re.match(r'.*[a-z]{2}', arr) or re.match(r'.*,,', arr)):
            arr = input('1: Re-enter the str into the array: ')
        else:
            break
    else:
        arr = input('2: Re-enter the str into the array: ')

strip_arr = arr.replace(',', '')
print(strip_arr)

# get all sub-string from a given string
all_subs = [strip_arr[i: j] for i in range(len(strip_arr)) for j in range(i + 1, len(strip_arr) + 1)]

# judge if have intersection
def inter(a,b):
    return list(set(a)&set(b))

max_num=0
for i in all_subs:
    for j in all_subs:
        if not inter(i,j):
            tmp_num = len(i)*len(j)
            if (max_num < tmp_num):
                max_num = tmp_num
print("The max num is %d"% max_num)

输出:

[~]$ ./test.py
Enter the str: 1
2: Re-enter the str into the array: a
2: Re-enter the str into the array: a,M
2: Re-enter the str into the array: ab,c
1: Re-enter the str into the array: a,,c,d
1: Re-enter the str into the array: a,.d,g
2: Re-enter the str into the array: a,c,t,h,h,f,y,k,q,y,y,u,i,g,k,s,k,l,s,a,h,
acthhfykqyyuigksklsah
The max num is 78
[~]$ ./test.py
Enter the str: a,c,t,h,h,f,y,k,q,y,y,u,i,g,k,s,k,l,s,a,h,f,h,j,u,a,t,i,o,p,x,n,h,k,l,w,h
acthhfykqyyuigksklsahfhjuatiopxnhklwh
The max num is 112
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值