- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – N sized substrings with K distinct characters
When it is required to split ‘N’ sized substrings with ‘K’ distinct characters, it is iterated over, and the ‘set’ method is used to get the different combinations.
Below is a demonstration of the same
my_string = 'Pythonisfun' print("The string is : ") print(my_string) my_substring = 2 my_chars = 2 my_result =  for idx in range(0, len(my_string) - my_substring + 1): if (len(set(my_string[idx: idx + my_substring])) == my_chars): my_result.append(my_string[idx: idx + my_substring]) print("The resultant string is : ") print(my_result)
The string is : Pythonisfun The resultant string is : ['Py', 'yt', 'th', 'ho', 'on', 'ni', 'is', 'sf', 'fu', 'un']
A string is defined and is displayed on the console.
A substring, and the characters are defined.
An empty list is defined.
The string is iterated with respect to the number in substring.
If the length of the unique characters in the string is equal to the characters, it is appended to the empty list.
This is the result which is displayed on the console.
- Python program to find N-sized substrings with K distinct characters
- Count number of substrings with exactly k distinct characters in C++
- Find K-Length Substrings With No Repeated Characters in Python
- Find all possible substrings after deleting k characters in Python
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Longest Substring with At Most K Distinct Characters in C++
- Distinct Echo Substrings in C++
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Program to find length of longest substring which contains k distinct characters in Python
- Find N distinct numbers whose bitwise Or is equal to K in Python
- Smallest Subsequence of Distinct Characters in Python
- Maximum count of substrings of length K consisting of same characters in C++
- Find all distinct pairs with difference equal to k in Python
- Python – Remove Elements in K distance with N