Skip to content

467. 环绕字符串中唯一的子字符串

Posted on:2022年10月26日 at 14:18

467. 环绕字符串中唯一的子字符串

leetcode 链接

解法一:动态规划

官方题解

/**
 * @param {string} p
 * @return {number}
 */
function findSubstringInWraproundString(p) {
  const dp = Array.from({ length: 26 }).fill(0)
  let k = 0
  for (let i = 0; i < p.length; i++) {
    if (i > 0 && (p[i].charCodeAt() - p[i - 1].charCodeAt() + 26) % 26 === 1) {
      k++
    }
    else {
      k = 1
    }
    dp[p[i].charCodeAt() - 'a'.charCodeAt()] = Math.max(
      dp[p[i].charCodeAt() - 'a'.charCodeAt()],
      k
    )
  }
  return _.sum(dp)
}