Last Updated: 30 Oct, 2025

Unique Substrings of Length k

Moderate

Problem statement

You are given a string password and an integer k. Your task is to find the number of distinct (unique) substrings of length exactly k that exist within the password.


A substring is a contiguous sequence of characters. For example, in the string "ababa" with k=3, the substrings of length 3 are "aba", "bab", and "aba". The set of unique substrings is {"aba", "bab"}, so the total count is 2.


Input Format:
The first line contains an integer k, the desired length of the substrings.

The second line contains the string password.


Output Format:
Print a single integer representing the count of unique substrings of length k.


Note:
The problem constraints make it important to use an efficient data structure for storing and counting the unique substrings. A hash set is an excellent choice for this, as it automatically handles duplicates.