FastPrepCount Distinct Fixed-Length Substrings

Count Distinct Fixed-Length Substrings

Adobe logoAdobe● MediumINTERNOA
Learn

Problem statement

Given a lowercase string text and an integer k, consider every contiguous substring of text whose length is exactly k.

Return the number of distinct substring values among those windows. Equal text appearing at different positions counts once.

Function

countDistinctSubstrings(text: String, k: int) → int

Examples

Example 1

text = "ababa"k = 2return = 2

The length-2 windows are ab, ba, ab, and ba. The distinct values are ab and ba.

Example 2

text = "aaaa"k = 2return = 1

Every length-2 window is aa, so there is one distinct value.

Example 3

text = "abc"k = 1return = 3

The one-character windows are a, b, and c, all distinct.

Constraints

  • 1 <= text.length <= 10^5.
  • text contains only lowercase English letters.
  • 1 <= k <= text.length.

Source note: These two source-faithful panels preserve the complete March 12 Adobe OA task and its set-based solution notes.

More Adobe problems

See Adobe hiring insights
public int countDistinctSubstrings(String text, int k) {
    // Write your code here.
}
text"ababa"
k2
expected2
Checking account…