美文网首页
字符串查找 js实现kmp算法

字符串查找 js实现kmp算法

作者: 爱吃猫的老虎 | 来源:发表于2021-02-23 15:42 被阅读0次

github地址: https://github.com/Tiger-eat-cat/search.ts

export const createLongPrefix = (str: string): string[] => {
    const prefixCollection: string[] = []
    const length: number = str.length
    if (length === 1) {
        return prefixCollection
    }
    for (let i = 0; i < length - 1; i++) {
        prefixCollection.push(str.slice(0, i + 1))
    }
    return prefixCollection
}

export const createLongSuffix = (str: string): string[] => {
    const suffixCollection: string[] = []
    const length: number = str.length
    if (length === 1) {
        return suffixCollection
    }
    for (let i = length - 1; i > 0; i--) {
        suffixCollection.unshift(str.slice(i, length))
    }
    return suffixCollection
}

export const intersectionLength = (arg1: string[], arg2: string[]): number => {
    let result = ''
    const arrayMap: Map<string, string> = new Map()
    arg1.forEach(item => arrayMap.set(item, item))
    for (let i = 0; i < arg2.length; i++) {
        const item = arg2[i]
        if (arrayMap.has(item)) {
            result = item
            break
        }
    }
    return result.length
}

export const buildKMPSearch = (search: string): Search => {
    const partMatchTable: Map<number, number> = new Map() // pmt
    for (let i = 0; i < search.length; i++) {
        const str = search.slice(0, i + 1)
        partMatchTable.set(i, intersectionLength(createLongSuffix(str), createLongPrefix(str)))
    }
    return {
        search: (content: string): SearchResult[] => {
            // debugger
            const contentLength = content.length
            const matchLength = search.length
            const result: SearchResult[] = []
            let i = 0
            let j = 0
            const calculateMatchIndex = (currentIndex: number): void => {
                // debugger
                j = j - (currentIndex - (partMatchTable.get(currentIndex - 1) as number))
            }
            const startMatch = () => {
                // debugger
                if (i < contentLength) {
                    if (content[i] !== search[j]) {
                        if (j !== 0) {
                            calculateMatchIndex(j)
                        } else {
                            i++
                        }
                    } else {
                        if (j === matchLength - 1) {
                            result.push({ start: i - (matchLength - 1), end: i })
                            i++
                            j = 0
                        } else {
                            i++
                            j++
                        }
                    }
                    startMatch()
                }
            }
            startMatch()
            return result
        }
    }
}

相关文章

  • 算法(2)KMP算法

    1.0 问题描述 实现KMP算法查找字符串。 2.0 问题分析 “KMP算法”是对字符串查找“简单算法”的优化。 ...

  • KMP算法文章合集

    字符串的查找:朴素查找算法和KMP算法 暴力匹配算法与KMP算法(串的匹配) 字符串查找算法BF和KMP 字符串匹...

  • JavaScript 二分查找 & KMP 算法

    KMP 查找 Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串 str1...

  • KMP(三) 字符串快速匹配示例

    概述:本文主要讲解KMP实现字符串快速查找的一个Demo;不了解KMP的同学可以参考:KMP(一) 模式匹配算法推...

  • 字符串查找 js实现kmp算法

    github地址: https://github.com/Tiger-eat-cat/search.ts[http...

  • KMP字符串匹配算法的实现

    KMP字符串匹配算法的实现 暴力查找 这是最简单的一种字符串匹配算法: 使用一个指针 i 跟踪目标文本 txt, ...

  • iOS-字符串查找

    字符串查找通常有四种方式,暴力查找,KMP查找,BoyerMoore查找以及RabinKarp算法查找,查找最简单...

  • 子字符串查找(1)

    一、定义 本文主要介绍子字符串查找的各类常用算法,如朴素匹配算法(暴力查找)、KMP算法、BM算法等。各类匹配算法...

  • 字符串匹配与KMP算法

    1.朴素字符串匹配算法 2.KMP算法 求前缀函数 实现KMP算法 3.测试代码

  • 分享几道常见字符串算法题

    1. KMP 算法 谈到字符串问题,不得不提的就是 KMP 算法,它是用来解决字符串查找的问题,可以在一个字符串(...

网友评论

      本文标题:字符串查找 js实现kmp算法

      本文链接:https://www.haomeiwen.com/subject/asnrfltx.html