📌  相关文章
📜  词典上最小的K长度子字符串,包含最大数量的元音

📅  最后修改于: 2021-06-26 20:31:26             🧑  作者: Mango

给定字符串str仅包含小写英文字母和整数K ,任务是找到一个K长度的子字符串,该子字符串包含最大数量的元音(即‘a’,’e’,’i’,’o’,’u’ )。如果有多个这样的子字符串,则返回字典上最小的子字符串。
例子:

天真的方法:
为了解决上述问题,我们必须生成所有长度为K的子串,并在字典上存储所有包含最大元音数量的子串中最小的子串。
时间复杂度: O(N 2 )
高效方法:
可以通过创建前缀和数组来优化上述过程 元音的pref [] ,其中ith索引包含从0ith索引的元音计数。任何子串str [l:r]的元音计数可以由pref [r] -pref [l-1]给出。然后,找到在词典上最小的子串,并带有最大数量的元音。
下面是上述方法的实现:

C++
// C++ program to find
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
 
#include 
using namespace std;
 
// Function that prints the
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
string maxVowelSubString(
    string str, int K)
{
    // Store the length of the string
    int N = str.length();
 
    // Initialize a prefix sum array
    int pref[N];
 
    // Loop through the string to
    // create the prefix sum array
    for (int i = 0; i < N; i++) {
 
        // Store 1 at the index
        // if it is a vowel
        if (str[i] == 'a'
            or str[i] == 'e'
            or str[i] == 'i'
            or str[i] == 'o'
            or str[i] == 'u')
            pref[i] = 1;
 
        // Otherwise, store 0
        else
            pref[i] = 0;
 
        // Process the prefix array
        if (i)
            pref[i] += pref[i - 1];
    }
 
    // Initialize the variable to store
    // maximum count of vowels
    int maxCount = pref[K - 1];
 
    // Initialize the variable
    // to store substring
    // with maximum count of vowels
    string res = str.substr(0, K);
 
    // Loop through the prefix array
    for (int i = K; i < N; i++) {
 
        // Store the current
        // count of vowels
        int currCount
            = pref[i]
              - pref[i - K];
 
        // Update the result if current count
        // is greater than maximum count
        if (currCount > maxCount) {
 
            maxCount = currCount;
            res = str.substr(i - K + 1, K);
        }
 
        // Update lexicographically smallest
        // substring if the current count
        // is equal to the maximum count
        else if (currCount == maxCount) {
 
            string temp
                = str.substr(
                    i - K + 1, K);
 
            if (temp < res)
                res = temp;
        }
    }
 
    // Return the result
    return res;
}
 
// Driver Program
int main()
{
    string str = "ceebbaceeffo";
    int K = 3;
 
    cout << maxVowelSubString(str, K);
 
    return 0;
}


Java
// Java program to find
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
class GFG{
 
// Function that prints the
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
static String maxVowelSubString(String str,
                                int K)
{
  // Store the length of the string
  int N = str.length();
 
  // Initialize a prefix sum array
  int []pref = new int[N];
 
  // Loop through the string to
  // create the prefix sum array
  for (int i = 0; i < N; i++)
  {
    // Store 1 at the index
    // if it is a vowel
    if (str.charAt(i) == 'a' ||
        str.charAt(i) == 'e' ||
        str.charAt(i) == 'i' ||
        str.charAt(i) == 'o' ||
        str.charAt(i) == 'u')
      pref[i] = 1;
 
    // Otherwise, store 0
    else
      pref[i] = 0;
 
    // Process the prefix array
    if (i != 0)
      pref[i] += pref[i - 1];
  }
 
  // Initialize the variable to store
  // maximum count of vowels
  int maxCount = pref[K - 1];
 
  // Initialize the variable
  // to store substring
  // with maximum count of vowels
  String res = str.substring(0, K);
 
  // Loop through the prefix array
  for (int i = K; i < N; i++)
  {
    // Store the current
    // count of vowels
    int currCount = pref[i] -
                    pref[i - K];
 
    // Update the result if current count
    // is greater than maximum count
    if (currCount > maxCount)
    {
      maxCount = currCount;
      res = str.substring(i - K + 1,
                          i + 1);
    }
 
    // Update lexicographically smallest
    // substring if the current count
    // is equal to the maximum count
    else if (currCount == maxCount)
    {
      String temp = str.substring(i - K + 1,
                                  i + 1);
 
      if (temp.compareTo(res) < 0)
        res = temp;
    }
  }
 
  // Return the result
  return res;
}
 
// Driver Code
public static void main(String []args)
{
  String str = "ceebbaceeffo";
  int K = 3;
  System.out.print(maxVowelSubString(str, K));
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program to find
# lexicographically smallest
# K-length substring containing
# maximum number of vowels
 
# Function that prints the
# lexicographically smallest
# K-length substring containing
# maximum number of vowels
def maxVowelSubString(str1, K):
     
    # Store the length of the string
    N = len(str1)
 
    # Initialize a prefix sum array
    pref = [0 for i in range(N)]
 
    # Loop through the string to
    # create the prefix sum array
    for i in range(N):
         
        # Store 1 at the index
        # if it is a vowel
        if (str1[i] == 'a' or
            str1[i] == 'e' or
            str1[i] == 'i' or
            str1[i] == 'o' or
            str1[i] == 'u'):
            pref[i] = 1
 
        # Otherwise, store 0
        else:
            pref[i] = 0
 
        # Process the prefix array
        if (i):
            pref[i] += pref[i - 1]
 
    # Initialize the variable to
    # store maximum count of vowels
    maxCount = pref[K - 1]
 
    # Initialize the variable
    # to store substring with
    # maximum count of vowels
    res = str1[0:K]
 
    # Loop through the prefix array
    for i in range(K, N):
         
        # Store the current
        # count of vowels
        currCount = pref[i] - pref[i - K]
 
        # Update the result if current count
        # is greater than maximum count
        if (currCount > maxCount):
            maxCount = currCount
            res = str1[i - K + 1 : i + 1]
 
        # Update lexicographically smallest
        # substring if the current count
        # is equal to the maximum count
        elif (currCount == maxCount):
            temp = str1[i - K + 1 : i + 1]
 
            if (temp < res):
                res = temp
 
    # Return the result
    return res
 
# Driver code
if __name__ == '__main__':
     
    str1 = "ceebbaceeffo"
    K = 3
 
    print(maxVowelSubString(str1, K))
 
# This code is contributed by Surendra_Gangwar


C#
// C# program to find
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
using System;
class GFG{
 
// Function that prints the
// lexicographically smallest
// K-length substring containing
// maximum number of vowels
static string maxVowelSubString(string str,
                                int K)
{
    // Store the length of the string
    int N = str.Length;
 
    // Initialize a prefix sum array
    int []pref = new int[N];
 
    // Loop through the string to
    // create the prefix sum array
    for (int i = 0; i < N; i++)
    {
 
        // Store 1 at the index
        // if it is a vowel
        if (str[i] == 'a' ||
            str[i] == 'e' ||
            str[i] == 'i' ||
            str[i] == 'o' ||
            str[i] == 'u')
            pref[i] = 1;
 
        // Otherwise, store 0
        else
            pref[i] = 0;
 
        // Process the prefix array
        if (i != 0)
            pref[i] += pref[i - 1];
    }
 
    // Initialize the variable to store
    // maximum count of vowels
    int maxCount = pref[K - 1];
 
    // Initialize the variable
    // to store substring
    // with maximum count of vowels
    string res = str.Substring(0, K);
 
    // Loop through the prefix array
    for (int i = K; i < N; i++)
    {
 
        // Store the current
        // count of vowels
        int currCount = pref[i] -
                        pref[i - K];
 
        // Update the result if current count
        // is greater than maximum count
        if (currCount > maxCount)
        {
            maxCount = currCount;
            res = str.Substring(i - K + 1, K);
        }
 
        // Update lexicographically smallest
        // substring if the current count
        // is equal to the maximum count
        else if (currCount == maxCount)
        {
            string temp = str.Substring(i - K + 1, K);
 
            if (string.Compare(temp, res) == -1)
                res = temp;
        }
    }
 
    // Return the result
    return res;
}
 
// Driver Code
public static void Main()
{
    string str = "ceebbaceeffo";
    int K = 3;
 
    Console.Write(maxVowelSubString(str, K));
}
}
 
// This code is contributed by Code_Mech


输出:
ace


时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。