1684838553 / arithmeticQuestions

程序员的算法趣题
2 stars 0 forks source link

无重复字符的连续子串的个数 #15

Open 1684838553 opened 2 years ago

1684838553 commented 2 years ago

6、无重复字符的连续子串的个数 输入:abac 输出:8 解释:所有子串是a ab aba abac b ba bac a ac ,无重复字符是:a ab b ba bac a ac c,总个数为8 输入:xbmxbnh 输出:21 解释:无重复字符是:x xb xbm b bm bmx m mx mxb mxbn mxbnh x xb xbn xbnh b bn bnh n nh h,总个数为21

1684838553 commented 2 years ago

let str1 = 'abac';
let str2 = 'xbmxbnh';

function GetCountOfSubString1(str) {
    let len = str.length
    let count = 0

    for(let i = 0;i<len ;i++){
        let l = 1
        while(l <= len - i){
            const char = str.substr(i, l)
            console.log(char, 'char')
            if(getStr(char)){
                count++
            }
            l++
        }
    }
    return count
}

function getStr(str){
    for(let i = 0;i<str.length;i++){
        if(str.indexOf(str[i]) !== str.lastIndexOf(str[i])){
            return false
        }
    }
    return true
}
console.log(GetCountOfSubString1(str2))