Special String Again

  • + 0 comments
    function substrCount(n, s) {
    
        let totalSpecialStrings = 0;
    
        for (let i = 0; i < n; i++) {
    				
            // find consecutive matches
            let start = i;
    								
            while (s[i] == s[i + 1] && i <= n) {
                i++;
                totalSpecialStrings++;
            }
            let end = i;
    
            //find substrings in consecutive matches of size 3 or more
            let sameChars = s.substring(start, end + 1);
            let seqSize = 3;
    				
            while (seqSize <= sameChars.length) {
                totalSpecialStrings += sameChars.length - seqSize + 1;
                seqSize++;
            };
    
            // find palindrome sandwhich
            let prevCharIndex = i - 1;
            let nextCharIndex = i + 1;
            let prevChars = s.substring(prevCharIndex, i);
            let nextChars = s.substring(i + 1, nextCharIndex + 1);
    				
            while (prevChars == nextChars) {
                totalSpecialStrings++;
                prevCharIndex--;
                nextCharIndex++;
                prevChars = s.substring(prevCharIndex, i);
                nextChars = s.substring(i + 1, nextCharIndex + 1);
    
            }
        }
        return totalSpecialStrings + n;
    }