📌  相关文章
📜  最小 K 使得每个长度至少为 K 的子串都包含一个字符c |第 2 组

📅  最后修改于: 2022-05-13 01:57:06.479000             🧑  作者: Mango

最小 K 使得每个长度至少为 K 的子串都包含一个字符c |第 2 组

给定一个由N个小写英文字母组成的字符串S ,同时给定一个字符C称为K-amazing ,如果每个长度至少为K的子串都包含该字符C,则任务是找到最小可能的K使得存在至少一个K-amazing字符。

例子:

对于 Naive 和 Binary Search 方法,请参阅Set 1

方法:可以基于以下观察优化朴素方法:对于每个长度为K的子串中存在字符“ C ”,两个连续的“ C ”位置之间的距离不能超过K。请按照以下步骤解决问题:

  • 初始化一个整数变量,比如ansN ,它将存储可能的子字符串的最小大小,使得每个大小为ans的子字符串至少有一个K-amazing 字符 。
  • 在字符串S的前后插入字符“ 0 ”。
  • 使用变量c遍历 [a, z] 范围内的字符并执行以下步骤:
    • 将字符c分配给S[0]S[N+1]。
    • 初始化两个变量,将prev设置为0 ,将maxLen 设置0,其中prev将存储字符c的最后一个索引,而maxLen将存储两个最近的c位置之间的最大距离。
    • 使用变量i遍历范围[1, N+1]并执行以下步骤:
      • 如果S[i] = c,则将maxLen的值修改为max(maxLen, i – prev)并且 然后将i分配给prev
    • 现在将ans的值修改为min(ans, max_len)
  • 最后,完成以上步骤后,打印得到的ans的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum value of K
// such that there exist atleast one K
// amazing character
 
int MinimumLengthSubstring(string S, int N)
{
    // Stores the answer
    int ans = N;
 
    // Update the string S
    S = "0" + S + "0";
 
    // Iterate over the characters
    // in range [a, z]
    for (char c = 'a'; c <= 'z'; ++c) {
 
        // Stores the last index where
        // c appears
        int prev = 0;
 
        // Stores the maximum possible length
        int max_len = 0;
 
        // Update string S
        S[0] = c;
        S[N + 1] = c;
 
        // Iterate over characters of string
        // S
        for (int i = 1; i <= N + 1; ++i) {
            // If S[i] is equal to c
            if (S[i] == c) {
 
                // Stores the distance between
                // positions of two same c
                int len = i - prev;
 
                // Update max_len
                max_len = max(max_len, len);
 
                // Update the value of prev
                prev = i;
            }
        }
 
        // Update the value of ans
        ans = min(ans, max_len);
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    string S = "abcde";
    int N = S.length();
 
    // Function Call
    cout << MinimumLengthSubstring(S, N);
}


Python3
# Python3 program for the above approach
 
# Function to find minimum value of K
# such that there exist atleast one K
# amazing character
def MinimumLengthSubstring(S, N):
     
    # Stores the answer
    ans = N
 
    # Update the S
    S = "0" + S + "0"
    S = [i for i in S]
 
    # Iterate over the characters
    # in range [a, z]
    for c in range(ord('a'), ord('z') + 1):
         
        # Stores the last index where
        # c appears
        prev = 0
 
        # Stores the maximum possible length
        max_len = 0
 
        # Update S
        S[0] = chr(c)
        S[N + 1] = chr(c)
 
        # Iterate over characters of string
        # S
        for i in range(1, N + 2):
             
            # If S[i] is equal to c
            if (S[i] == chr(c)):
                 
                # Stores the distance between
                # positions of two same c
                len = i - prev
 
                # Update max_len
                max_len = max(max_len, len)
 
                # Update the value of prev
                prev = i
 
        # Update the value of ans
        ans = min(ans, max_len)
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    S = "abcde"
    N = len(S)
 
    # Function Call
    print(MinimumLengthSubstring(S, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find minimum value of K
// such that there exist atleast one K
// amazing character
 
static int MinimumLengthSubstring(string S, int N)
{
    // Stores the answer
    int ans = N;
 
    // Update the string S
    S = "0" + S + "0";
 
    // Iterate over the characters
    // in range [a, z]
    for (char c = 'a'; c <= 'z'; ++c) {
 
        // Stores the last index where
        // c appears
        int prev = 0;
 
        // Stores the maximum possible length
        int max_len = 0;
 
        // Update string S
       S = S.Substring(0,0) + c + S.Substring(1);
       S = S.Substring(0, N+1) + c + S.Substring(N + 2);
 
        // Iterate over characters of string
        // S
        for (int i = 1; i <= N + 1; ++i) {
            // If S[i] is equal to c
            if (S[i] == c) {
 
                // Stores the distance between
                // positions of two same c
                int len = i - prev;
 
                // Update max_len
                max_len = Math.Max(max_len, len);
 
                // Update the value of prev
                prev = i;
            }
        }
 
        // Update the value of ans
        ans = Math.Min(ans, max_len);
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Given Input
    string S = "abcde";
    int N = S.Length;
 
    // Function Call
    Console.Write(MinimumLengthSubstring(S, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(1)