算法(TS): 实现 strStr()

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

示例 1:

输入:haystack = "sadbutsad", needle = "sad"

输出:0

解释:"sad" 在下标 0 和 6 处匹配。

第一个匹配项的下标是 0 ,所以返回 0 。

示例 2:

输入:haystack = "leetcode", needle = "leeto"

输出:-1

解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。

提示:

  • 1 <= haystack.length, needle.length <= 104
  • haystack 和 needle 仅由小写英文字符组成

解答

遍历 haystack,当 haystack[i] === needle[0]时,再比较 haystack[i + 1] 与 needle[1]是否相等,haystack[i + 2] 与 needle[2] 是否相等,一直到 haystack[i + needle.length - 1] 与 needle[needle.length - 1] 是否相等。这个过程一共有两层遍历,当 haystack[i] === needle[0] 满足时,才开始第二层遍历

ini 复制代码
function strStr(haystack: string, needle: string): number {
    let index = -1
    let i = 0
    while(i < haystack.length) {
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < needle.length) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i++
    }

    return index
};

空间复杂度O(1),时间负责度O(m * n)

needle 是 haystack 的一部分,意味着 haystack 的长度不小于 needle 的长度,因此对上面的解答进行优化

ini 复制代码
function strStr(haystack: string, needle: string): number {
    let index = -1
    let i = 0
    while(i < haystack.length) {
        if (haystack.length - i < needle.length) {
            break
        }
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < needle.length) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i++
    }

    return index
};

在上述解法中,每一次遇到不匹配的位置都是将 needle 往后移动一步,利用KMP算法计算出部分匹配表,动态计算需要移动的步数

ini 复制代码
function strStr(haystack: string, needle: string): number {
    const n = haystack.length, m = needle.length;
    let index = -1
    let i = 0
    // 部分匹配表
    const pi = new Array(m).fill(0);
    for (let i = 1, j = 0; i < m; i++) {
        while (j > 0 && needle[i] !== needle[j]) {
            j = pi[j - 1];
        }
        if (needle[i] == needle[j]) {
            j++;
        }
        pi[i] = j;
    }

    while(i < n) {
        let j = 1
        if (n - i < m) {
            break
        }
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < m) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    // needle 移动步数 = 已匹配的字符数 - 对应的部分匹配值
                    j = step - pi[step-1]
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i += j
    }

    return index
};

时间复杂度O(m+n),空间复杂度O(m)

相关推荐
NAGNIP40 分钟前
一文搞懂机器学习中的特征降维!
算法·面试
NAGNIP1 小时前
一文搞懂机器学习中的特征构造!
算法·面试
Learn Beyond Limits1 小时前
解构语义:从词向量到神经分类|Decoding Semantics: Word Vectors and Neural Classification
人工智能·算法·机器学习·ai·分类·数据挖掘·nlp
你怎么知道我是队长2 小时前
C语言---typedef
c语言·c++·算法
Qhumaing3 小时前
C++学习:【PTA】数据结构 7-1 实验7-1(最小生成树-Prim算法)
c++·学习·算法
Z1Jxxx5 小时前
01序列01序列
开发语言·c++·算法
汽车仪器仪表相关领域6 小时前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试
Doro再努力7 小时前
【数据结构08】队列实现及练习
数据结构·算法
刘一说8 小时前
TypeScript 与 JavaScript:现代前端开发的双子星
javascript·ubuntu·typescript