📜  查找最多包含 K 个正常字符的最长子串的长度

📅  最后修改于: 2021-09-04 08:05:11             🧑  作者: Mango

给定一个由小英文字母组成的字符串P和一个26 位比特字符串Q ,其中1 代表特殊字符0 代表26 个英文字母的正常字符。任务是找到最多 K 个正常字符的最长子串的长度。

例子:

方法:
为了解决上面提到的问题,我们将使用两个指针的概念。因此,维护子串的左右指针,以及正常字符的计数。增加正确的索引,直到正常字符的数量最多为 K。然后用到目前为止遇到的最大长度的子串更新答案。增加左索引并减少计数,直到它大于 K。
下面是上述方法的实现:

C++
// C++ implementation to Find
// length of longest substring
// with at most K normal characters
#include 
using namespace std;
 
// Function to find maximum
// length of normal substrings
int maxNormalSubstring(string& P, string& Q,
                       int K, int N)
{
 
    if (K == 0)
        return 0;
 
    // keeps count of normal characters
    int count = 0;
 
    // indexes of substring
    int left = 0, right = 0;
 
    // maintain length of longest substring
    // with at most K normal characters
    int ans = 0;
 
    while (right < N) {
 
        while (right < N && count <= K) {
 
            // get position of character
            int pos = P[right] - 'a';
 
            // check if current character is normal
            if (Q[pos] == '0') {
 
                // check if normal characters
                // count exceeds K
                if (count + 1 > K)
 
                    break;
 
                else
                    count++;
            }
 
            right++;
 
            // update answer with substring length
            if (count <= K)
                ans = max(ans, right - left);
        }
 
        while (left < right) {
 
            // get position of character
            int pos = P[left] - 'a';
 
            left++;
 
            // check if character is
            // normal then decrement count
            if (Q[pos] == '0')
 
                count--;
 
            if (count < K)
                break;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    // initialise the string
    string P = "giraffe", Q = "01111001111111111011111111";
 
    int K = 2;
 
    int N = P.length();
 
    cout << maxNormalSubstring(P, Q, K, N);
 
    return 0;
}


Java
// Java implementation to Find
// length of longest subString
// with at most K normal characters
class GFG{
  
// Function to find maximum
// length of normal subStrings
static int maxNormalSubString(char []P, char []Q,
                       int K, int N)
{
  
    if (K == 0)
        return 0;
  
    // keeps count of normal characters
    int count = 0;
  
    // indexes of subString
    int left = 0, right = 0;
  
    // maintain length of longest subString
    // with at most K normal characters
    int ans = 0;
  
    while (right < N) {
  
        while (right < N && count <= K) {
  
            // get position of character
            int pos = P[right] - 'a';
  
            // check if current character is normal
            if (Q[pos] == '0') {
  
                // check if normal characters
                // count exceeds K
                if (count + 1 > K)
  
                    break;
  
                else
                    count++;
            }
  
            right++;
  
            // update answer with subString length
            if (count <= K)
                ans = Math.max(ans, right - left);
        }
  
        while (left < right) {
  
            // get position of character
            int pos = P[left] - 'a';
  
            left++;
  
            // check if character is
            // normal then decrement count
            if (Q[pos] == '0')
  
                count--;
  
            if (count < K)
                break;
        }
    }
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    // initialise the String
    String P = "giraffe", Q = "01111001111111111011111111";
  
    int K = 2;
  
    int N = P.length();
  
    System.out.print(maxNormalSubString(P.toCharArray(), Q.toCharArray(), K, N));
}
}
 
// This code is contributed by Princi Singh


Python3
# Function to find maximum
# length of normal substrings
def maxNormalSubstring(P, Q, K, N):
     
    if (K == 0):
        return 0
   
    # keeps count of normal characters
    count = 0
   
    # indexes of substring
    left, right = 0, 0
     
    # maintain length of longest substring
    # with at most K normal characters
    ans = 0
   
    while (right < N):
   
        while (right < N and count <= K):
   
            # get position of character
            pos = ord(P[right]) - ord('a')
   
            # check if current character is normal
            if (Q[pos] == '0'):
   
                # check if normal characters
                # count exceeds K
                if (count + 1 > K):
                    break
                else:
                    count += 1
   
            right += 1
   
            # update answer with substring length
            if (count <= K):
                ans = max(ans, right - left)
   
        while (left < right):
   
            # get position of character
            pos = ord(P[left]) - ord('a')
   
            left += 1
   
            # check if character is
            # normal then decrement count
            if (Q[pos] == '0'):
                count -= 1
   
            if (count < K):
                break
   
    return ans
   
# Driver code
if(__name__ == "__main__"):
    # initialise the string
    P = "giraffe"
    Q = "01111001111111111011111111"
   
    K = 2
   
    N = len(P)
   
    print(maxNormalSubstring(P, Q, K, N))
 
# This code is contributed by skylags


C#
// C# implementation to Find
// length of longest subString
// with at most K normal characters
using System;
 
public class GFG{
 
// Function to find maximum
// length of normal subStrings
static int maxNormalSubString(char []P, char []Q,
                    int K, int N)
{
 
    if (K == 0)
        return 0;
 
    // keeps count of normal characters
    int count = 0;
 
    // indexes of subString
    int left = 0, right = 0;
 
    // maintain length of longest subString
    // with at most K normal characters
    int ans = 0;
 
    while (right < N) {
 
        while (right < N && count <= K) {
 
            // get position of character
            int pos = P[right] - 'a';
 
            // check if current character is normal
            if (Q[pos] == '0') {
 
                // check if normal characters
                // count exceeds K
                if (count + 1 > K)
 
                    break;
 
                else
                    count++;
            }
 
            right++;
 
            // update answer with subString length
            if (count <= K)
                ans = Math.Max(ans, right - left);
        }
 
        while (left < right) {
 
            // get position of character
            int pos = P[left] - 'a';
 
            left++;
 
            // check if character is
            // normal then decrement count
            if (Q[pos] == '0')
 
                count--;
 
            if (count < K)
                break;
        }
    }
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    // initialise the String
    String P = "giraffe", Q = "01111001111111111011111111";
 
    int K = 2;
 
    int N = P.Length;
 
    Console.Write(maxNormalSubString(P.ToCharArray(),
                     Q.ToCharArray(), K, N));
}
}
 
// This code contributed by Princi Singh


Javascript


输出:
3

时间复杂度:上述方法需要 O(N) 时间。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live