📜  使用Binary Search由元音组成的最长子串

📅  最后修改于: 2021-04-24 15:20:18             🧑  作者: Mango

给定长度为N的字符串str ,任务是使用二进制搜索技术找到最长的仅包含元音的子字符串。
例子:

方法:有关O(N)复杂度的方法,请参考元音的最长子串。
二进制搜索方法:在本文中,我们使用的是基于二进制搜索的方法:
请按照以下步骤解决问题:

  1. 在1到N的长度范围内应用二进制搜索。
  2. 对于每个值,检查是否存在长度为mid的子串,该子串中仅由元音组成。
  3. 如果存在长度为mid的子串,则更新max的值并将l更新为mid + 1,以检查是否存在长度大于mid的子串,该子串仅由元音组成。
  4. 如果中间长度的没有这样的字符串存在,更新R作为中间-1,以检查是否大于中间长度小的子串的存在与否,其仅由元音。
  5. 重复上述三个步骤,直到l小于或等于r。
  6. 返回最终获得的最大长度。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to check if a character
// is vowel or not
bool vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4
        || vo == 8 || vo == 14
        || vo == 20)
        return true;
    else
        return false;
}
 
// Function to check if any
// substring of length k exists
// which contains only vowels
bool check(string s, int k)
{
    vector cnt(26, 0);
    for (int i = 0; i < k - 1; i++) {
        cnt[s[i] - 'a']++;
    }
 
    // Applying sliding window to get
    // all substrings of length K
    for (int i = k - 1; i < s.size();
         i++) {
        cnt[s[i] - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++) {
            if (vowel(j) == false
                && cnt[j] > 0) {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
 
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s[i - k + 1] - 'a']--;
    }
 
    return false;
}
 
// Function to perform  Binary Search
int longestSubstring(string s)
{
    int l = 1, r = s.size();
    int maxi = 0;
 
    // Doing binary search on the lengths
    while (l <= r) {
        int mid = (l + r) / 2;
        if (check(s, mid)) {
            l = mid + 1;
            maxi = max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
 
// Driver Code
int main()
{
    string s = "sedrewaefhoiu";
    cout << longestSubstring(s);
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function to check if a character
// is vowel or not
static boolean vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4 ||
        vo == 8 || vo == 14 ||
        vo == 20)
        return true;
    else
        return false;
}
 
// Function to check if any
// subString of length k exists
// which contains only vowels
static boolean check(String s, int k)
{
    int []cnt = new int[26];
    for (int i = 0; i < k - 1; i++)
    {
        cnt[s.charAt(i) - 'a']++;
    }
 
    // Applying sliding window to get
    // all subStrings of length K
    for (int i = k - 1; i < s.length(); i++)
    {
        cnt[s.charAt(i) - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++)
        {
            if (vowel(j) == false && cnt[j] > 0)
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
 
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s.charAt(i - k + 1) - 'a']--;
    }
 
    return false;
}
 
// Function to perform Binary Search
static int longestSubString(String s)
{
    int l = 1, r = s.length();
    int maxi = 0;
 
    // Doing binary search on the lengths
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (check(s, mid))
        {
            l = mid + 1;
            maxi = Math.max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "sedrewaefhoiu";
    System.out.print(longestSubString(s));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of
# the above approach
 
# Function to check if a character
# is vowel or not
def vowel(vo):
 
    # 0-a 1-b 2-c and so on 25-z
    if (vo == 0 or vo == 4 or
        vo == 8 or vo == 14 or
        vo == 20):
        return True
    else:
        return False
 
# Function to check if any
# substring of length k exists
# which contains only vowels
def check(s, k):
 
    cnt = [0] * 26
    for i in range (k - 1):
        cnt[ord(s[i]) - ord('a')] += 1
    
    # Applying sliding window to get
    # all substrings of length K
    for i in range (k - 1, len(s)):
        cnt[ord(s[i]) - ord('a')] += 1
        flag1 = 0
        for j in range (26):
            if (vowel(j) == False
                and cnt[j] > 0):
                flag1 = 1
                break
         
        if (flag1 == 0):
            return True
 
        # Remove the occurence of
        # (i-k+1)th character
        cnt[ord(s[i - k + 1]) - ord('a')] -= 1
 
    return False
 
# Function to perform  Binary Search
def longestSubstring(s):
 
    l = 1
    r = len(s)
    maxi = 0
 
    # Doing binary search on the lengths
    while (l <= r):
        mid = (l + r) // 2
        if (check(s, mid)):
            l = mid + 1
            maxi = max(maxi, mid)
        else:
            r = mid - 1
    return maxi
 
# Driver Code
if __name__ == "__main__": 
    s = "sedrewaefhoiu"
    print (longestSubstring(s))
 
# This code is contributed by Chitranayal


C#
// C# implementation of
// the above approach
using System;
class GFG{
  
// Function to check if a character
// is vowel or not
static bool vowel(int vo)
{
    // 0-a 1-b 2-c and so on 25-z
    if (vo == 0 || vo == 4 ||
        vo == 8 || vo == 14 ||
        vo == 20)
        return true;
    else
        return false;
}
  
// Function to check if any
// subString of length k exists
// which contains only vowels
static bool check(String s, int k)
{
    int []cnt = new int[26];
    for (int i = 0; i < k - 1; i++)
    {
        cnt[s[i] - 'a']++;
    }
  
    // Applying sliding window to get
    // all subStrings of length K
    for (int i = k - 1; i < s.Length; i++)
    {
        cnt[s[i] - 'a']++;
        int flag1 = 0;
        for (int j = 0; j < 26; j++)
        {
            if (vowel(j) == false && cnt[j] > 0)
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            return true;
  
        // Remove the occurence of
        // (i-k+1)th character
        cnt[s[i - k + 1] - 'a']--;
    }
  
    return false;
}
  
// Function to perform Binary Search
static int longestSubString(String s)
{
    int l = 1, r = s.Length;
    int maxi = 0;
  
    // Doing binary search on the lengths
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (check(s, mid))
        {
            l = mid + 1;
            maxi = Math.Max(maxi, mid);
        }
        else
            r = mid - 1;
    }
    return maxi;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "sedrewaefhoiu";
    Console.Write(longestSubString(s));
}
}
 
// This code is contributed by sapnasingh4991


输出:
3



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