Minimum value of K such that each substring of size K has the given character
Last Updated :
23 Feb, 2023
Given a string of lowercase letters S a character c. The task is to find minimum K such that every substring of length K contains the given character c. If there is no such K possible, return -1.
Examples:
Input: S = “abdegb”, ch = ‘b’
Output: 4
Explanation:
Consider the value of K as 4. Now, every substring of size K(= 4) are {“abde”, “bdeg”, “degb” } has the character ch(= b’).
Input: S = “abfge”, ch = ‘m’
Output : -1
Naive Approach: The simplest approach to solve the given problem is to iterate for all possible sizes of substrings over the range [1, N] and check which minimum value of K satisfies the given criteria. If there doesn’t exist any such value of K, then print “-1”.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
bool ispresent( int i,string s, char c)
{
int j=0;
unordered_map< int , int > mm;
for ( int k=0;k<s.length();k++)
{
mm[s[k]]++;
if (k-j+1==i)
{
if (mm==0) return false ;
mm[s[j]]--;
j++;
}
}
return true ;
}
int findK(string s, char c)
{
for ( int i=1;i<=s.length();i++)
{
if (ispresent(i,s,c)) return i;
}
return -1;
}
int main() {
string S = "abdegb" ;
char ch = 'b' ;
cout<<(findK(S, ch));
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean ispresent( int i, String s, char c) {
int j = 0 ;
Map<Character, Integer> mm = new HashMap<>();
for ( int k = 0 ; k < s.length(); k++) {
char currentChar = s.charAt(k);
mm.put(currentChar, mm.getOrDefault(currentChar, 0 ) + 1 );
if (k - j + 1 == i) {
if (!mm.containsKey(c))
return false ;
int count = mm.get(s.charAt(j));
if (count == 1 ) {
mm.remove(s.charAt(j));
} else {
mm.put(s.charAt(j), count - 1 );
}
j++;
}
}
return true ;
}
static int findK(String s, char c) {
for ( int i = 1 ; i <= s.length(); i++) {
if (ispresent(i, s, c))
return i;
}
return - 1 ;
}
public static void main(String[] args) {
String S = "abdegb" ;
char ch = 'b' ;
System.out.println(findK(S, ch));
}
}
|
Python3
def ispresent(i, s, c):
j = 0
mm = {}
for k in range ( len (s)):
if s[k] in mm:
mm[s[k]] + = 1
else :
mm[s[k]] = 1
if k - j + 1 = = i:
if c not in mm:
return False
mm[s[j]] - = 1
if mm[s[j]] = = 0 :
del mm[s[j]]
j + = 1
return True
def findK(s, c):
for i in range ( 1 , len (s) + 1 ):
if ispresent(i, s, c):
return i
return - 1
S = "abdegb"
ch = "b"
print (findK(S, ch))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static bool ispresent( int i, string s, char c)
{
int j = 0;
Dictionary< char , int > mm = new Dictionary< char , int >();
for ( int k = 0; k < s.Length; k++)
{
if (!mm.ContainsKey(s[k]))
{
mm[s[k]] = 0;
}
mm[s[k]]++;
if (k - j + 1 == i)
{
if (mm.ContainsKey(c) && mm == 0) return false ;
mm[s[j]]--;
j++;
}
}
return true ;
}
static int findK( string s, char c)
{
for ( int i = 1; i <= s.Length; i++)
{
if (ispresent(i, s, c)) return i;
}
return -1;
}
public static void Main()
{
string S = "abdegb" ;
char ch = 'b' ;
Console.WriteLine(findK(S, ch));
}
}
|
Javascript
function ispresent(i, s, c) {
let j = 0;
let mm = {};
for (let k = 0; k < s.length; k++) {
if (mm[s[k]]) {
mm[s[k]] += 1;
} else {
mm[s[k]] = 1;
}
if (k - j + 1 == i) {
if (!mm) {
return false ;
}
mm[s[j]] -= 1;
if (mm[s[j]] == 0) {
delete mm[s[j]];
}
j += 1;
}
}
return true ;
}
function findK(s, c) {
for (let i = 1; i <= s.length; i++) {
if (ispresent(i, s, c)) {
return i;
}
}
return -1;
}
let S = "abdegb" ;
let ch = "b" ;
console.log(findK(S, ch));
|
Time Complexity: O(N^2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using an observation that the minimum value of K is equal to the maximum difference between the consecutive occurrences of the given character ch as for every substring of size K there must have at least 1 character as ch. Follow the steps below to solve the given problem:
- Initialize a variable, say maxDifference as -1 that store the resultant value of K.
- Initialize a variable, say previous as 0 that store the previous occurrence of the character ch in the string S.
- Traverse the given string S using the variable i and if the current character is ch then update the value of maxDifference to the maximum of maxDifference and (i – previous) and the value of previous to i.
- After completing the above steps, print the value of maxDifference as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findK(string s, char c)
{
int n = s.size();
int diff;
int max = 0;
int prev = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == c) {
diff = i - prev;
prev = i;
if (diff > max) {
max = diff;
}
}
}
if (max == 0)
return -1;
return max;
}
int main() {
string S = "abdegb" ;
char ch = 'b' ;
cout<<(findK(S, ch));
return 0;
}
|
Java
class GFG {
public static int findK(String s, char c)
{
int n = s.length();
int diff;
int max = 0 ;
int prev = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == c) {
diff = i - prev;
prev = i;
if (diff > max) {
max = diff;
}
}
}
if (max == 0 )
return - 1 ;
return max;
}
public static void main(String args[]) {
String S = "abdegb" ;
char ch = 'b' ;
System.out.println(findK(S, ch));
}
}
|
Python3
def findK(s, c):
n = len (s)
diff = 0
max = 0
prev = 0
for i in range ( 0 , n):
if (s[i] = = c):
diff = i - prev
prev = i
if (diff > max ):
max = diff
if ( max = = 0 ):
return - 1
return max
if __name__ = = "__main__" :
S = "abdegb"
ch = 'b'
print (findK(S, ch))
|
C#
using System.Collections.Generic;
using System;
class GFG
{
public static int findK( string s, char c)
{
int n = s.Length;
int diff;
int max = 0;
int prev = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == c) {
diff = i - prev;
prev = i;
if (diff > max) {
max = diff;
}
}
}
if (max == 0)
return -1;
return max;
}
public static void Main()
{
string S = "abdegb" ;
char ch = 'b' ;
Console.WriteLine(findK(S, ch));
}
}
|
Javascript
<script>
function findK(s, c)
{
let n = s.length;
let diff;
let max = 0;
let prev = 0;
for (let i = 0; i < n; i++) {
if (s[i] == c) {
diff = i - prev;
prev = i;
if (diff > max) {
max = diff;
}
}
}
if (max == 0) return -1;
return max;
}
let S = "abdegb" ;
let ch = "b" ;
document.write(findK(S, ch));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...