P25 · 1 second · 256 MB

Distinct Substrings of Length K

Return how many **distinct** contiguous substrings of `s` have length exactly `k`. If `k` is 0 or `k > s.length`, return 0.

function distinctSubstringsK(s, k) → number

CONSTRAINTS

  • 0 ≤ s.length ≤ 2000
  • 0 ≤ k ≤ 2000
  • s contains only lowercase letters a-z
Input: s = 'abab', k = 2
Output: 2
ab, ba, ab — unique are ab and ba.
Input: s = 'aaaa', k = 1
Output: 1
Input: s = 'abc', k = 4
Output: 0
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.