【发布时间】:2012-06-30 00:32:41
【问题描述】:
(我是在 JavaScript 的上下文中编写的,但会接受任何语言的算法正确答案)
如何在字符串数组中找到每个元素的最短子字符串,其中子字符串不包含在任何其他元素中,忽略大小写?
假设我有一个输入数组,例如:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
输出应该是这样的:
var uniqueNames = ["ne", "h", "ua", "ka", "i", "r"];
出于我的目的,您可以放心地假设没有元素会完全包含在另一个元素中。
我的想法:
似乎有人可能会按照以下方式强制执行此操作:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], nameInd, windowSize, substrInd, substr, otherNameInd, foundMatch;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
foundMatch = false;
// For each other name
for (otherNameInd = 0; otherNameInd < names.length; otherNameInd++)
{
if (nameInd != otherNameInd && names[otherNameInd].toLowerCase().indexOf(substr) > -1)
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
// This substr works!
uniqueNames[nameInd] = substr;
break windowLoop;
}
}
}
}
但我必须想象有一个更优雅的解决方案,使用尝试/前缀树、后缀数组或类似的东西。
编辑: 我相信这是所选答案在 JavaScript 中以编程方式采用的形式:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], permutations = {}, permutation, nameInd, windowSize, substrInd, substr;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
permutations[substr] = (typeof permutations[substr] === "undefined")?nameInd:-1;
}
}
}
for (substr in permutations)
{
permutation = permutations[substr];
if (permutation !== -1 && ((typeof uniqueNames[permutation] === "string" && substr.length < uniqueNames[permutation].length) || typeof uniqueNames[permutation] === "undefined"))
{
uniqueNames[permutation] = substr;
}
}
【问题讨论】:
-
您的样本输出不正确吗?我在那里看不到
s和y,而看到i, h和r... -
@Icarus 啊,好点子。
s和y不存在只是因为我不是在寻找所有符合标准的最小子字符串,而是任何一个都足够好。我会接受一个返回所有它们的二维数组的答案,但我真的不需要那种详细程度的细节。同样有效的输出可能是var uniqueNames = ["ne", "y", "ua", "ka", "i", "s"]; -
是否可以将您的输入字母限制为 26 个字符(或类似的,只是限制它)?
-
@SaeedAmiri 我不太确定你要走哪条路线,但我的实际输入仅包含 [0-9a-zA-Z_-'&,\.\s] 中的字符在输入中,您可以将输出限制为仅包含字母数字字符,尽管我可能会选择限制较少的答案而不是限制较多的答案,你知道吗?
-
@Patrick 有一个使用后缀数组的 O(M) 解决方案;其中 M 是所有字符串的长度之和。
标签: arrays string algorithm unique substring