
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find K-Length Substrings With No Repeated Characters in Python
Suppose we have a string S, we have to find the number of substrings of length K where no characters are repeated. So if S = “heyfriendshowareyou” and K is 5, then output will be 15, as the strings are [heyfr, eyfri, yfrie, frien, riend, iends, endsh, ndsho, dshow, showa, howar, oware, warey, areyo, reyou]
To solve this, we will follow these steps −
- create one empty map m, and left := 0 and right := -1 and ans := 0
- while right < length of string – 1
- if right – left + 1 = k, then
- increase ans by 1
- decrease m[str[left]] by 1
- increase left by 1
- continue to next iteration
- if str[right + 1] is not in m, then
- set m[str[right + 1]] := 1
- increase right by 1
- else if m[str[right + 1]] is 0, then
- increase m[str[right + 1]] by 1
- increase right by 1
- else
- decrease m[str[left]] by 1
- left := left + 1
- if right – left + 1 = k, then
- if right – left + 1 = k, then increase ans by 1
- return ans
Example
Let us see the following implementation to get a better understanding −
class Solution(object): def numKLenSubstrNoRepeats(self, S, K): m = {} left = 0 right = -1 ans = 0 while right<len(S)-1: if right - left + 1 == K: ans+=1 m[S[left]]-=1 left+=1 continue if S[right+1] not in m : m[S[right+1]]=1 right+=1 elif not m[S[right+1]]: m[S[right+1]]+=1 right+=1 else: m[S[left]]-=1 left+=1 if right - left + 1 == K: ans+=1 return ans ob1 = Solution() print(ob1.numKLenSubstrNoRepeats("heyfriendshowareyou", 5))
Input
"heyfriendshowareyou" 5
Output
"AIIOC"
- Related Articles
- Python – N sized substrings with K distinct characters
- Python program to find N-sized substrings with K distinct characters
- Find all possible substrings after deleting k characters in Python
- Maximum count of substrings of length K consisting of same characters in C++
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Count number of substrings with exactly k distinct characters in C++
- Program to find length of longest sublist containing repeated numbers by k operations in Python
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find length of substring with consecutive common characters in Python
- Find the longest substring with k unique characters in a given string in Python
- Find distinct characters in distinct substrings of a string
- Python Program to Capitalize repeated characters in a string
- Program to check pattern of length m repeated K or more times exists or not in Python
- Count substrings with same first and last characters in C++

Advertisements