# -*- coding:utf-8 -*- class LongestString: def getLongest(self, s, n): # write code here s.sort(key=lambda x:len(x),reverse = True) # 按长度排序 def panduan(string, s): # 判断该字符串是否可以由其他字符串组成 if string == '': return True for x in s: if string.startswith(x): if panduan(string[len(x):],s): return True return False for i in range(n): # 从左到右遍历每一个字符串 word = s[i] if panduan(word, s[i+1:]): # 如果找到可以有其他字符串组成,直接返回长度 return len(word)