📜  恰好包含 K 个元音的最长子串

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

恰好包含 K 个元音的最长子串

给定字符串str包含大写和小写字母,以及一个整数K 。任务是找到包含恰好K个元音(可能是重复的)的最长子串。

例子:

方法:可以通过跟踪当前窗口中遇到的元音数量来解决该任务。
请按照以下步骤解决问题:

  • 创建一个变量' vow '来跟踪当前窗口中元音的数量
  • 开始增加窗口的大小,如果vow变得大于K ,则从前面开始缩小窗口。
  • 在每一步中最大化窗口的大小

下面是上述方法的实现:

C++14
// C++ program to find the longest
// substring with exactly K vowels.
#include 
using namespace std;
 
#define MAX 128
 
// Function to check whether
// a character is vowel or not
bool isVowel(char x)
{
    return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
            || x == 'u' || x == 'A' || x == 'E' || x == 'I'
            || x == 'O' || x == 'U');
}
 
// Function to find the length of the longest
// substring with k vowels
void get(string s, int k)
{
 
    // Stores the length of longest
    // substring with K vowels
    int ans = -1;
 
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    // Stores the resultant string
    string res;
    int l = 0, r = 0;
 
    while (r < s.length()) {
        if (isVowel(s[r]))
            vow++;
        if (vow == k) {
            if (ans < r - l + 1) {
                ans = max(ans, r - l + 1);
                res = s.substr(l, r - l + 1);
            }
        }
        if (vow > k) {
            while (vow > k) {
                if (isVowel(s[l]))
                    vow--;
                l++;
            }
            if (ans < r - l + 1) {
                ans = max(ans, r - l + 1);
                res = s.substr(l, r - l + 1);
            }
        }
        r++;
    }
    cout << ans << " " << res;
}
 
// Driver code
int main(void)
{
    string s = "TrueGeek";
    int K = 3;
    get(s, K);
    return 0;
}


Java
// Java code to implement above approach
class GFG {
 
  // Function to check whether
  // a character is vowel or not
  static boolean isVowel(char x)
  {
    return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
            || x == 'u' || x == 'A' || x == 'E'
            || x == 'I' || x == 'O' || x == 'U');
  }
 
  // Function to find the length of the longest
  // substring with k vowels
  static void get(String s, int k)
  {
 
    // Stores the length of longest
    // substring with K vowels
    int ans = -1;
 
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    // Stores the resultant string
    String res = "";
    int l = 0, r = 0;
 
    while (r < s.length()) {
      if (isVowel(s.charAt(r)))
        vow++;
      if (vow == k) {
        if (ans < r - l + 1) {
          ans = Math.max(ans, r - l + 1);
          res = s.substring(l, r - l + 1);
        }
      }
      if (vow > k) {
        while (vow > k) {
          if (isVowel(s.charAt(l)))
            vow--;
          l++;
        }
        if (ans < r - l + 1) {
          ans = Math.max(ans, r - l + 1);
          res = s.substring(l, r - l + 1);
        }
      }
      r++;
    }
    System.out.print(ans + " " + res);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    String s = "TrueGeek";
    int K = 3;
    get(s, K);
  }
}
 
// This code is contributed by ukasp.


Python3
# Python code for the above approach
MAX = 128
 
# Function to check whether
# a character is vowel or not
def isVowel(x):
    return (x == 'a' or x == 'e' or x == 'i' or x == 'o'
            or x == 'u' or x == 'A' or x == 'E' or x == 'I'
            or x == 'O' or x == 'U')
 
# Function to find the length of the longest
# substring with k vowels
def get(s, k):
 
    # Stores the length of longest
    # substring with K vowels
    ans = -1
 
    # Stores the number of vowels
    # in the current window
    vow = 0
 
    # Stores the resultant string
    res = None
    l = 0
    r = 0
 
    while (r < len(s)):
        if (isVowel(s[r])):
            vow += 1
        if (vow == k):
            if (ans < r - l + 1):
                ans = max(ans, r - l + 1)
                res = s[l:(r - l + 1)]
        if (vow > k):
            while (vow > k):
                if (isVowel(s[l])):
                    vow -= 1
                l += 1
            if (ans < r - l + 1):
                ans = max(ans, r - l + 1)
                res = s[l: (r - l + 1)]
        r += 1
 
    print(f"{ans} {res}")
 
# Driver code
s = "TrueGeek"
K = 3
get(s, K)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to implement above approach
using System;
class GFG
{
   
// Function to check whether
// a character is vowel or not
static bool isVowel(char x)
{
    return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
            || x == 'u' || x == 'A' || x == 'E' || x == 'I'
            || x == 'O' || x == 'U');
}
 
// Function to find the length of the longest
// substring with k vowels
static void get(string s, int k)
{
 
    // Stores the length of longest
    // substring with K vowels
    int ans = -1;
 
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    // Stores the resultant string
    string res = "";
    int l = 0, r = 0;
 
    while (r < s.Length) {
        if (isVowel(s[r]))
            vow++;
        if (vow == k) {
            if (ans < r - l + 1) {
                ans = Math.Max(ans, r - l + 1);
                res = s.Substring(l, r - l + 1);
            }
        }
        if (vow > k) {
            while (vow > k) {
                if (isVowel(s[l]))
                    vow--;
                l++;
            }
            if (ans < r - l + 1) {
                ans = Math.Max(ans, r - l + 1);
                res = s.Substring(l, r - l + 1);
            }
        }
        r++;
    }
    Console.Write(ans + " " + res);
}
 
// Driver code
public static void Main()
{
    string s = "TrueGeek";
    int K = 3;
    get(s, K);
     
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
6 TrueGe

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