【发布时间】:2012-08-09 17:53:28
【问题描述】:
今天小弟问了我一个问题,问题如下:
Given a list of strings & string M28K, where M28K represents a string which starts
from M, ends with K and has 28chars in between . Find if M28K is unique in the
list of strings or not?
我想出了以下算法来找到问题的解决方案:
对于每个字符串:
find string length(L)
if(L==30) then
if(str[0]=='M' && str[L-1]=='K') then
verify rest of 28 characters are matching or not
就时间复杂度而言,该解决方案似乎效率不高。谁能给出一个更好的算法来解决这个问题?
【问题讨论】:
-
定义“在字符串列表中是唯一的”。这是否意味着“M28K 只能在字符串列表中找到一次”,或者“M28K 不存在于字符串列表中”?无论哪种情况,我认为您能做的最好的事情就是 O(n) 时间,这就是您的解决方案!干得好。
-
efficient way to search for string in list of string? 的可能重复项或网站上的 100 个其他问题,询问如何在列表中查找字符串。我们知道它以什么字母开头和结尾的事实是无关紧要的。
-
@Kevin “在字符串列表中是唯一的”意味着,给定的字符串列表根本不包含
M28K。 -
@Kevin Jaguars 算法不是 O(n)。请看我的解释。