VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-k-length-substrings-with-no-repeated-characters/

⇱ Count K-Length Substrings With No Repeated Characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count K-Length Substrings With No Repeated Characters

Last Updated : 11 Oct, 2024

Given a string S and an integer k, the task is to return the number of substrings in S of length k with no repeated characters.

Example:

Input: S = "geeksforgeeks", k = 5
Output: 4
Explanation: There are 4 substrings, they are: 'eksfo', 'ksfor', 'sforg', 'forge'.

Input: S = "home", k = 5
Output: 0
Explanation: Notice k can be larger than the length of S. In this case, it is not possible to find any substring.

Approach:

We iterate over the input, expanding and contracting our window.

  • Whenever the window has a repeating character, contract the window until there are no repeating characters.
  • If length of window equals K, add 1 to count of good windows and then contract the window.
    • This works because since we checked and got rid of repeating characters in step 1, our window of length K must NOT have any repeating characters.
    • We contract our window in this step because we don't want a window that has a length GREATER than K.

Below points we also have to keep in mind:

  • Keep track of our window bounds with left and right.
  • Use a frequency dictionary to keep track of how many times we've seen a character.
  • Remember to access frequency dictionary freq[S[right]] and not just freq[right]
  • Check if right - left + 1 == K because we've added right to our frequency dictionary but we haven't actually incremented right yet

Steps-by-step approach:

  • Initialize variables:
    • Create an unordered_map to store the frequency of each character in the current window.
    • Initialize result to 0, which will store the number of substrings of length K with no repeating characters.
    • Initialize left and right pointers to 0, which will represent the current window.
  • Slide the window:
    • Iterate through the string S using the right pointer.
    • For each character S[right], increment its frequency in the freq map.
    • Remove duplicates:
  • While the frequency of the current character S[right] is greater than 1 (i.e., there is a duplicate):
    • Decrement the frequency of the leftmost character S[left] in the freq map.
    • Increment the left pointer to move the left side of the window.
  • Count good windows:
    • If the size of the current window (right - left + 1) is equal to K, it means we have found a substring of length K with no repeating characters.
    • Increment the result.
    • Decrement the frequency of the leftmost character S[left] in the freq map and increment the left pointer to slide the window.
  • Increment the right pointer:
    • Increment the right pointer to move the right side of the window.
  • Return the final result.

Below is the implementation of the above approach:


Output
Number of substrings of length 5 with no repeating characters: 4

Time Complexity: O(n), where n is the length of the string and k is window size
Auxiliary Space: O(min(n, K))

Comment
Article Tags: