code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

04 Substrings Of Size Three With Distinct Characters

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const substringsOfSizeThreeWithDistinctCharacters = str => {
  if (str && str.length < 3) {
    return 0
  }
  let result = 0
  let s = [str[0], str[1]]
  for (let right = 2; right < str.length; right++) {
    s.push(str[right])
    if (new Set(s).size == 3) {
      result += 1
    }
    s.shift()
  }
  return result
}

test('substringsOfSizeThreeWithDistinctCharacters', () => {
  expect(substringsOfSizeThreeWithDistinctCharacters('aababcabc')).toEqual(5)
})

Created 2022-02-04T04:03:39+00:00 · Edit