Skip to content

Latest commit

 

History

History
41 lines (35 loc) · 799 Bytes

1759.统计同构子字符串的数目.md

File metadata and controls

41 lines (35 loc) · 799 Bytes

1759.统计同构子字符串的数目

/*
 * @lc app=leetcode.cn id=1759 lang=typescript
 *
 * [1759] 统计同构子字符串的数目
 */

// @lc code=start
function countHomogenous(s: string): number {}
// @lc code=end

解法 1: 组合计数

function countHomogenous(s: string): number {
  const n = s.length,
    MOD = 1e9 + 7
  let res = 0
  for (let i = 0, j = 0; i < n; i = ++j) {
    while (s[j + 1] === s[j]) j++
    let len = j - i + 1
    res = (res + (len * (len + 1)) / 2) % MOD
  }
  return res
}

Case

test.each([
  { input: { s: 'abbcccaa' }, output: 13 },
  { input: { s: 'xy' }, output: 2 },
  { input: { s: 'zzzzz' }, output: 15 },
])('input: s = $input.s', ({ input: { s }, output }) => {
  expect(countHomogenous(s)).toEqual(output)
})