Minimum K such that every substring of length at least K contains a character c | Set-2
Given a string S consisting of N lowercase English alphabets, and also given that a character C is called K-amazing, if every substring of length at least K contains this character C, the task is to find the minimum possible K such that there exists at least one K-amazing character.
Examples:
Input: S = “abcde”
Output: 3
Explanation:
Every, substring of length at least 3, has one K-amazing character, ‘c’: {“abc”, “bcd”, “cde”, “abcd”, “bcde”, “abcde”}.
Input: S = “aaaa”
Output: 1
Explanation:
Every, substring of length at least 1, has one K-amazing character, ‘a’: {“a”, “aa”, “aaa”, “aaaa”}.
For Naive and Binary Search approach, refer Set 1
Approach: The naive approach can be optimized based on the observation that for a character ‘C‘ to exist in every substring of length K, the distance between positions of two consecutive ‘C‘ cannot exceed K. Follow the steps below to solve the problem:
- Initialize an integer variable, say ans as N, which will store the minimum size of the substring possible such that every substring of size ans has at least one K-amazing character.
- Insert the character ‘0‘ to the front and end of the string S.
- Iterate over the characters in the range [a, z] using the variable c and perform the following steps:
- Assign character c to S[0] and S[N+1].
- Initialize two variables, say prev as 0 and maxLen as 0, where prev will store the last index of the character c and maxLen will store the maximum distance between the positions of the two nearest c.
- Iterate over the range [1, N+1] using the variable i and perform the following steps:
- If S[i] = c, then modify the value of maxLen to max(maxLen, i – prev) and then assign i to prev.
- Now modify the value of ans to min(ans, max_len).
- Finally, after completing the above steps, print the value of ans obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumLengthSubstring(string S, int N)
{
int ans = N;
S = "0" + S + "0" ;
for ( char c = 'a' ; c <= 'z' ; ++c) {
int prev = 0;
int max_len = 0;
S[0] = c;
S[N + 1] = c;
for ( int i = 1; i <= N + 1; ++i) {
if (S[i] == c) {
int len = i - prev;
max_len = max(max_len, len);
prev = i;
}
}
ans = min(ans, max_len);
}
return ans;
}
int main()
{
string S = "abcde" ;
int N = S.length();
cout << MinimumLengthSubstring(S, N);
}
|
Java
import java.util.*;
public class Main {
static int MinimumLengthSubstring(String S, int N)
{
int ans = N;
S = "0" + S + "0" ;
for ( char c = 'a' ; c <= 'z' ; ++c) {
int prev = 0 ;
int max_len = 0 ;
S = c + S.substring( 1 , S.length() - 1 ) + c;
for ( int i = 1 ; i <= N + 1 ; ++i) {
if (S.charAt(i) == c) {
int len = i - prev;
max_len = Math.max(max_len, len);
prev = i;
}
}
ans = Math.min(ans, max_len);
}
return ans;
}
public static void main(String[] args)
{
String S = "abcde" ;
int N = S.length();
System.out.println(MinimumLengthSubstring(S, N));
}
}
|
Python3
def MinimumLengthSubstring(S, N):
ans = N
S = "0" + S + "0"
S = [i for i in S]
for c in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
prev = 0
max_len = 0
S[ 0 ] = chr (c)
S[N + 1 ] = chr (c)
for i in range ( 1 , N + 2 ):
if (S[i] = = chr (c)):
len = i - prev
max_len = max (max_len, len )
prev = i
ans = min (ans, max_len)
return ans
if __name__ = = '__main__' :
S = "abcde"
N = len (S)
print (MinimumLengthSubstring(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MinimumLengthSubstring( string S, int N)
{
int ans = N;
S = "0" + S + "0" ;
for ( char c = 'a' ; c <= 'z' ; ++c) {
int prev = 0;
int max_len = 0;
S = S.Substring(0,0) + c + S.Substring(1);
S = S.Substring(0, N+1) + c + S.Substring(N + 2);
for ( int i = 1; i <= N + 1; ++i) {
if (S[i] == c) {
int len = i - prev;
max_len = Math.Max(max_len, len);
prev = i;
}
}
ans = Math.Min(ans, max_len);
}
return ans;
}
public static void Main()
{
string S = "abcde" ;
int N = S.Length;
Console.Write(MinimumLengthSubstring(S, N));
}
}
|
Javascript
function MinimumLengthSubstring(S, N) {
let ans = N;
S = "0" + S + "0" ;
S = S.split( "" )
for (let c = 'a' .charCodeAt(0); c <= 'z' .charCodeAt(0); ++c) {
let prev = 0;
let max_len = 0;
S[0] = String.fromCharCode(c);
S[N + 1] = String.fromCharCode(c);
for (let i = 1; i <= N + 1; ++i) {
if (S[i] == String.fromCharCode(c)) {
let len = i - prev;
max_len = Math.max(max_len, len);
prev = i;
}
}
ans = Math.min(ans, max_len);
}
return ans;
}
let S = "abcde" ;
let N = S.length;
console.log(MinimumLengthSubstring(S, N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
22 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...