我使用这个可怕且低效的实现来找到可以删除最多连续最后一个字母的单词并且仍然是一个单词.
例如,Rodeo是众所周知的:Rodeo,Rode,Rod,Ro.该计划找到了"作曲家":作曲家,作曲家,作曲家,作曲家,作曲家
我不知道我应该如何去创建一个发现可以有任何字母(不只是最后的)移除,它仍然被认为是一个字的最长的单词的程序:
例如:野兽,最好,下注,是 - 将是一个有效的可能性
这是我的程序,找到删除连续字母的那个(我也有兴趣听听如何改进和优化):
#Recursive function that finds how many letters can be removed from a word and
#it still be valid.
def wordCheck(word, wordList, counter):
if len(word)>=1:
if word in wordList:
return (wordCheck(word[0:counter-1], wordList, counter-1))
else:
return counter
return counter
def main():
a = open('C:\\Python32\\megalist2.txt', 'r+')
wordList = set([line.strip() for line in a])
#megaList contains a sorted list of tuple of
#(the word, how many letters can be removed consecutively)
megaList = sorted([(i, len(i)-1- wordCheck(i, wordList, …Run Code Online (Sandbox Code Playgroud) 给定一个字符串A和另一个字符串B.查找B的任何排列是否作为A的子字符串存在.
例如,
如果A ="百科全书"
如果B ="dep"则返回true,因为ped是dep的排列,ped是A的子串.
My solution->
if length(A)=n and length(B)=m
I did this in 0((n-m+1)*m) by sorting B and then checking A
with window size of m each time.
Run Code Online (Sandbox Code Playgroud)
我需要找到一个更好,更快的解决方案.
我需要找到所有可以用字符串中的字母组成的英语单词
sentence="Ziegler's Giant Bar"
Run Code Online (Sandbox Code Playgroud)
我可以制作一系列字母
sentence.split(//)
Run Code Online (Sandbox Code Playgroud)
如何从Ruby中的句子中创建超过4500个英语单词?
[编辑]
最好将问题分成几部分:
algorithm ×2
cpu-word ×1
knuth ×1
performance ×1
permutation ×1
python ×1
ruby ×1
string ×1
substring ×1