K 的最小值,使得每个大小为 K 的子字符串都具有给定的字符
给定字符串小写字母S一个字符c 。任务是找到最小K使得每个长度为K的子串都包含给定的字符c 。如果没有这样的K可能,则返回-1 。
例子:
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
朴素方法:解决给定问题的最简单方法是在[1, N]范围内迭代所有可能大小的子字符串,并检查K的哪个最小值满足给定标准。如果不存在任何这样的K值,则打印“-1” 。
时间复杂度: O(N 4 )
辅助空间: O(N)
有效方法:上述方法也可以通过使用观察来优化,即K的最小值等于给定字符ch的连续出现之间的最大差异,因为对于每个大小为K的子字符串,必须至少有1 个字符频道请按照以下步骤解决给定的问题:
- 初始化一个变量,比如maxDifference为-1 ,它存储K的结果值。
- 初始化一个变量,比如previous为0 ,它存储字符串S中字符ch的上一次出现。
- 使用变量i遍历给定的字符串S ,如果当前字符是ch ,则将 maxDifference的值更新为maxDifference和(i – previous)的最大值,并将previous的值更新为i 。
- 完成上述步骤后,打印maxDifference的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum value
// of K such that char c occurs in all
// K sized substrings of string S
int findK(string s, char c)
{
// Store the string length
int n = s.size();
// Store difference of lengths
// of segments of every two
// consecutive occurrences of c
int diff;
// Stores the maximum difference
int max = 0;
// Store the previous occurence
// of char c
int prev = 0;
for (int i = 0; i < n; i++) {
// Check if the current character
// is c or not
if (s[i] == c) {
// Stores the difference of
// consecutive occurrences of c
diff = i - prev;
// Update previous occurrence
// of c with current occurence
prev = i;
// Comparing diff with max
if (diff > max) {
max = diff;
}
}
}
// If string doesn't contain c
if (max == 0)
return -1;
// Return max
return max;
}
// Driver Code
int main() {
string S = "abdegb";
char ch = 'b';
cout<<(findK(S, ch));
return 0;
}
// This code is contributed by 29AjayKumar
Java
// Java program for the above approach
class GFG {
// Function to find the minimum value
// of K such that char c occurs in all
// K sized substrings of string S
public static int findK(String s, char c)
{
// Store the string length
int n = s.length();
// Store difference of lengths
// of segments of every two
// consecutive occurrences of c
int diff;
// Stores the maximum difference
int max = 0;
// Store the previous occurence
// of char c
int prev = 0;
for (int i = 0; i < n; i++) {
// Check if the current character
// is c or not
if (s.charAt(i) == c) {
// Stores the difference of
// consecutive occurrences of c
diff = i - prev;
// Update previous occurrence
// of c with current occurence
prev = i;
// Comparing diff with max
if (diff > max) {
max = diff;
}
}
}
// If string doesn't contain c
if (max == 0)
return -1;
// Return max
return max;
}
// Driver Code
public static void main(String args[]) {
String S = "abdegb";
char ch = 'b';
System.out.println(findK(S, ch));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
# Function to find the minimum value
# of K such that char c occurs in all
# K sized substrings of string S
def findK(s, c):
# Store the string length
n = len(s)
# Store difference of lengths
# of segments of every two
# consecutive occurrences of c
diff = 0
# Stores the maximum difference
max = 0
# Store the previous occurence
# of char c
prev = 0
for i in range(0, n):
# Check if the current character
# is c or not
if (s[i] == c):
# Stores the difference of
# consecutive occurrences of c
diff = i - prev
# Update previous occurrence
# of c with current occurence
prev = i
# Comparing diff with max
if (diff > max):
max = diff
# If string doesn't contain c
if (max == 0):
return -1
# Return max
return max
# Driver Code
if __name__ == "__main__":
S = "abdegb"
ch = 'b'
print(findK(S, ch))
# This code is contributed by rakeshsahni
C#
using System.Collections.Generic;
using System;
class GFG
{
// Function to find the minimum value
// of K such that char c occurs in all
// K sized substrings of string S
public static int findK(string s, char c)
{
// Store the string length
int n = s.Length;
// Store difference of lengths
// of segments of every two
// consecutive occurrences of c
int diff;
// Stores the maximum difference
int max = 0;
// Store the previous occurence
// of char c
int prev = 0;
for (int i = 0; i < n; i++) {
// Check if the current character
// is c or not
if (s[i] == c) {
// Stores the difference of
// consecutive occurrences of c
diff = i - prev;
// Update previous occurrence
// of c with current occurence
prev = i;
// Comparing diff with max
if (diff > max) {
max = diff;
}
}
}
// If string doesn't contain c
if (max == 0)
return -1;
// Return max
return max;
}
// Driver Code
public static void Main()
{
string S = "abdegb";
char ch = 'b';
Console.WriteLine(findK(S, ch));
}
}
// This code is contributed by amreshkumar3.
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(1)