字符串计算无重复的最长字符串长度

const str = 'hello world'
    function getStrNum(val) {
        let num = 0
        let arr = []
        let arrVal = []
        let strLength = val.length
        // if (strLength < 1) {
        for (let i = 0; i <= strLength; i++) {
            if (val[i] != val[i + 1]) {
                num++
            } else {
                arr.push(num)
                arrVal.push(val.slice((i - num), i))
                num = 0
            }
        }
        const getMaxNum = Math.max.apply(null, arr)
        const getMaxNumFun = () => {
            return getMaxNum
        }
        const getMaxStrFun = () => {
            return arrVal
        }
        return {
            getMaxNumFun: getMaxNumFun,
            getMaxStrFun: getMaxStrFun
        }
    }
    const getStrObj = getStrNum(str)
    console.log(getStrObj.getMaxNumFun())
    console.log(getStrObj.getMaxStrFun())

 

posted @ 2020-08-09 23:00  问问大将军  阅读(147)  评论(0编辑  收藏  举报