javascript实现:在N个字符串中找出最长的公子串

'use strict'

module.exports = function 找出最长公子串 (...strings) {
    let setsOfSubstrings = []
    strings.reduce((accumulator, currentValue) => {
        setsOfSubstrings.push(findAllSubStringsFromTwoString(accumulator, currentValue))
        return accumulator
    })
    return findTheLongest(findIntersection(...setsOfSubstrings))
}

function findIntersection (...sets) {
    /**
     *
     *
     * @param {sets} a list of sets
     * @returns {array} the elements of the intersection of given sets.
     */
    return sets.reduce((accumulator, currentValue) => [...accumulator].filter(value => currentValue.has(value)))
}

function findAllSubStringsFromTwoString (string1, string2) {
    let allSubstrings = new Set()
    for (let index = 0; index < string1.length; index++) {
        let length = 1, substring = string1.substr(index, length)
        while (string2.indexOf(substring) !== -1 && length <= string1.length) { // 当string2内含有substring时
            allSubstrings.add(substring)
            substring = string1.substr(index, length++)
        }
    }
    return allSubstrings
}

function findTheLongest (strings) {
    return strings.reduce((accumulator, currentValue) => findTheLonger(accumulator, currentValue), '')
}

function findTheLonger (string1, string2) {
    return (string1.length > string2.length) ? (string1) : (string2)
}

之前我给出的程序代码是错误的,没想到居然还被一些博客转载了。。

posted @ 2018-11-12 13:26  穆城雪  阅读(470)  评论(0)    收藏  举报