已知一个字符串数组words,要求寻找其中两个没有重复字符的字符串,使得这两个字符串的长度乘积最大,输出这个最大的乘积。如:
words=["abcd","wxyh","defgh"], 其中不包含重复字符的两个字符串是"abcd"和"wxyh",则输出16
words=["a","aa","aaa","aaaa"], 找不到满足要求的两个字符串,则输出0
数据范围:输入的字符串长度满足 ,保证只包含小写字母
Input:
["a","ab","abc","cd","bcd","abcd"]
Output:
4
["a","ab","abc","cd","bcd","abcd"]
4
Input中,不包含相同字符的有三对:
"ab"和"cd"
"a"和"cd"
"a"和"bcd"
所以字符串长度乘积的最大值是4
class Solution(object): def __init__(self): self.longest = 0 def findDiff(self,s1,s2): #判断两个字符串是否有重复的字符 if not s1&nbs***bsp;not s2: return False for i in s1: for j in s2: if i==j: return False return True def maxLen(self): #枚举所有的字符对进行比较,如果是没有重复的字符就判断字符长度的乘积与现有的做大值做比对, #如果超过就改变最大值 words = eval(input()) for i in range(len(words)): for j in range(i+1,len(words)): if self.findDiff(words[i],words[j]): if self.longest<len(words[i])*len(words[j]): self.longest=len(words[i])*len(words[j]) def main(): s = Solution() ***axLen() print(s.longest) main()
def fun_strip(x): return x[1:-1] input_str = input()[1:-1] words = list(map(fun_strip, input_str.split(","))) length = len(words) words_length = [] max_value = 0 for i in range(0, length): words_length.append(len(words[i])) for i in range(length-1): str1 = words[i] for j in range(i+1, length): flags = 0 for ch in words[j]: if ch in str1: flags = 1 break if flags == 0: new_value = words_length[i] * words_length[j] if new_value > max_value: max_value = new_value print(max_value)
""" 遍历所有情况判断 """ import sys def hava_same_char(a, b): for c in a: if c in b: return True return False if __name__ == "__main__": # sys.stdin = open("input.txt", "r") s = [c[1:-1] for c in input().strip()[1:-1].split(',')] ans = 0 for i in range(len(s)): for j in range(i + 1, len(s)): if not hava_same_char(s[i], s[j]): ans = max(ans, len(s[i]) * len(s[j])) print(ans)