C# 或 JavaScript:确定字符串中的公共前缀

发布于 2024-12-08 12:08:05 字数 427 浏览 0 评论 0原文

可能的重复:
查找字符串的公共前缀

假设我有一个字符串数组。该数组至少有两个元素,也可能有更多。数组中的每个字符串至少有两个字符,也可能更多,并且至少每个字符串的第一个字符是相同的。

给定所述字符串数组,确定所有字符串共享的最长前缀的最有效方法是什么?

例如,给定 ['cowgirl', 'cowboy'],公共前缀为 cow。或者,给定 ['a', 'abs', apples'],公共前缀是 a

Possible Duplicate:
Find common prefix of strings

Let's say I have an array of strings. The array has at least two elements, and possibly more. Each string in the array has at least two characters, and possibly more, and at least the first character of every string is the same.

Given said string array, what's the most efficient way to determine the longest prefix shared across all strings?

For example, given ['cowgirl', 'cowboy'], the common prefix is cow. Or, given ['a', 'abs', apples'], the common prefix is a.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2024-12-15 12:08:05

我认为最简单的算法,也许也是最有效的算法,就是按顺序检查单词,将迄今为止最长的公共前缀与每个单词进行比较,以确定可以保留多少单词。使用第一个字符串作为初始前缀。

编辑:下面是一些执行此操作的 Javascript 代码:

function findLongestPrefix(list) {
    var prefix = list[0];
    var prefixLen = prefix.length;
    for (var i = 1; i < list.length && prefixLen > 0; i++) {
        var word = list[i];
        // The next line assumes 1st char of word and prefix always match.
        // Initialize matchLen to -1 to test entire word.
        var matchLen = 0;
        var maxMatchLen = Math.min(word.length, prefixLen);
        while (++matchLen < maxMatchLen) {
            if (word.charAt(matchLen) != prefix.charAt(matchLen)) {
                break;
            }
        }
        prefixLen = matchLen;
    }
    return prefix.substring(0, prefixLen);
}

I think the simplest algorithm, and perhaps the most efficient, is to just examine the words in sequence, comparing the longest common prefix so far to each word to determine how much of it you can keep. Use the first string as the initial prefix.

EDIT: Here's some Javascript code that does the work:

function findLongestPrefix(list) {
    var prefix = list[0];
    var prefixLen = prefix.length;
    for (var i = 1; i < list.length && prefixLen > 0; i++) {
        var word = list[i];
        // The next line assumes 1st char of word and prefix always match.
        // Initialize matchLen to -1 to test entire word.
        var matchLen = 0;
        var maxMatchLen = Math.min(word.length, prefixLen);
        while (++matchLen < maxMatchLen) {
            if (word.charAt(matchLen) != prefix.charAt(matchLen)) {
                break;
            }
        }
        prefixLen = matchLen;
    }
    return prefix.substring(0, prefixLen);
}
☆獨立☆ 2024-12-15 12:08:05

您可以检查这些资源,了解如何查找两个或多个字符串之间的最长公共前缀子字符串。即使这些算法以其他语言提供,我相信您也可以将其改编为 JavaScript。

查找字符串的公共前缀

算法实现/字符串/最长公共子串

You can check these resources about finding the longest common prefix substring between two strings or more.. even if the Algorithms are presented in other languages I am sure you can adapt one to JavaScript.

Find common prefix of strings

Algorithm Implementation/Strings/Longest common substring

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文