Open pwstrick opened 4 years ago
647. 回文子串
/** * @param {string} s * @return {number} */ var countSubstrings = function(s) { let len = s.length, count = 0; for(let i=0; i<len; i++) { for(let j=len-i; j>0; j--) { let sub = s.substr(i, j); //i起始位置,j提取数量 if(isValid(sub)) { count++; } } } return count; }; function isValid(str) { let len = str.length; if(len == 1 || len == 0) { return true; } let start = str[0], end = str[len - 1]; if(start != end) { return false; } return isValid(str.substr(1, len-2)); }
647. 回文子串