
- 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 the longest substring with k unique characters in a given string in Python
Suppose we have a string we have to return the longest possible substring that has exactly k number of unique characters, if there are more than one substring of longest possible length, return any of them.
So, if the input is like s = "ppqprqtqtqt", k = 3, then the output will be rqtqtqt as that has length 7.
To solve this, we will follow these steps −
N := 26
Define a function is_ok() . This will take count, k
val := 0
for i in range 0 to N, do
if count[i] > 0, then
val := val + 1
return true when (k >= val)
From the main method, do the following −
unique := 0, size := size of s
count := An array of size N, fill with 0
for i in range 0 to size, do
if count of s[i] is same as 0, then
unique := unique + 1
increase count of s[i] by 1
if unique < k, then
there is no such character and exit
start := 0, end := 0
window_length := 1, window_start := 0
count := An array of size N, fill with 0
increase count of s[0] by 1
for i in range 1 to size, do
increase count of s[i] by 1
end := end + 1
while is_ok(count, k) is false, do
decrease count of s[i] by 1
start := start + 1
if end-start+1 > window_length, then
window_length := end-start+1
window_start := start
return substring of s[from index window_start to window_start + window_length]
Example
Let us see the following implementation to get better understanding −
N = 26 def is_ok(count, k): val = 0 for i in range(N): if count[i] > 0: val += 1 return (k >= val) def k_unique_chars(s, k): unique = 0 size = len(s) count = [0] * N for i in range(size): if count[ord(s[i])-ord('a')] == 0: unique += 1 count[ord(s[i])-ord('a')] += 1 if unique < k: return "Not sufficient characters" start = 0 end = 0 window_length = 1 window_start = 0 count = [0] * len(count) count[ord(s[0])-ord('a')] += 1 for i in range(1,size): count[ord(s[i])-ord('a')] += 1 end+=1 while not is_ok(count, k): count[ord(s[start])-ord('a')] -= 1 start += 1 if end-start+1 > window_length: window_length = end-start+1 window_start = start return s[window_start:window_start + window_length] s = "ppqprqtqtqt" k = 3 print(k_unique_chars(s, k))
Input
"ppqprqtqtqt", 3
Output
rqtqtqt
- Related Articles
- Longest Substring with At Least K Repeating Characters in C++
- Longest Substring with At Most K Distinct Characters in C++
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find minimum required chances to form a string with K unique characters in Python
- Longest Substring Without Repeating Characters in Python
- Find k longest words in given list in Python
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Program to find length of longest repeating substring in a string in Python
- Longest Substring with At Most Two Distinct Characters in C++
- Program to find length of longest substring with character count of at least k in Python
- Finding the longest Substring with At Least n Repeating Characters in JavaScript
- Longest string with two distinct characters in JavaScript
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find longest awesome substring in Python
- Longest Palindromic Substring in Python
