📌  相关文章
📜  查找包含所有元音的子串

📅  最后修改于: 2021-10-27 08:17:41             🧑  作者: Mango

我们得到了一个小写字母的字符串。我们需要至少打印一次包含所有元音的子串,并且子串中不存在辅音(非元音字符)。

例子:

Input : str = aeoibddaeoiud
Output : aeoiu

Input : str = aeoibsddaeiouudb
Output : aeiou, aeiouu

参考:-三星面试题。

我们使用基于散列的技术并从头开始遍历字符串。对于每个字符,我们考虑从它开始的所有子字符串。如果我们遇到一个辅音,我们就会移动到下一个起始字符。否则,我们在散列中插入当前字符。如果包含所有元音,则打印当前子字符串。

C++
// CPP program to find all substring that
// contain all vowels
#include 
 
using namespace std;
 
// Returns true if x is vowel.
bool isVowel(char x)
{
    // Function to check whether a character is
    // vowel or not
    return (x == 'a' || x == 'e' || x == 'i' ||
                        x == 'o' || x == 'u');
}
 
void FindSubstring(string str)
{
    set hash; // To store vowels
 
    // Outer loop picks starting character and
    // inner loop picks ending character.
    int n = str.length();
    for (int i = 0; i < n; i++) {
       for (int j = i; j < n; j++) {
 
            // If current character is not vowel,
            // then no more result substrings
            // possible starting from str[i].
            if (isVowel(str[j]) == false)
              break;
 
            // If vowel, then we insert it in hash             
            hash.insert(str[j]);
 
            // If all vowels are present in current
            // substring
            if (hash.size() == 5)
                cout << str.substr(i, j-i+1) << " ";
        }
 
        hash.clear();
    }
}
 
// Driver code
int main()
{
    string str = "aeoibsddaeiouudb";
    FindSubstring(str);
    return 0;
}


Java
// Java program to find all substring that 
// contain all vowels
import java.util.HashSet;
 
public class GFG {
 
    // Returns true if x is vowel.
    static boolean isVowel(char x) {
        // Function to check whether a character is
        // vowel or not
        return (x == 'a' || x == 'e' || x == 'i'
                || x == 'o' || x == 'u');
    }
 
    static void findSubstring(String str) {
        HashSet hash = new HashSet();
            // To store vowels
 
        // Outer loop picks starting character and
        // inner loop picks ending character.
        int n = str.length();
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
 
                // If current character is not vowel,
                // then no more result substrings
                // possible starting from str[i].
                if (isVowel(str.charAt(j)) == false)
                    break;
 
                // If vowel, then we insert it in hash
                hash.add(str.charAt(j));
 
                // If all vowels are present in current
                // substring
                if (hash.size() == 5)
                    System.out.print(str.substring(i, j + 1) + " ");
            }
            hash.clear();
        }
    }
 
    // Driver code
    public static void main(String[] args) {
        String str = "aeoibsddaeiouudb";
        findSubstring(str);
    }
}


Python3
# Python3 program to find all subthat
# contain all vowels
 
# Returns true if x is vowel.
def isVowel(x):
     
    # Function to check whether a character is
    # vowel or not
    if (x == 'a' or x == 'e' or x == 'i' or
        x == 'o' or x == 'u'):
        return True
    return False
 
def FindSubstr1ing(str1):
 
    # To store vowels
 
    # Outer loop picks starting character and
    # inner loop picks ending character.
    n = len(str1)
    for i in range(n):
        hash = dict()
        for j in range(i, n):
 
            # If current character is not vowel,
            # then no more result substr1ings
            # possible starting from str1[i].
            if (isVowel(str1[j]) == False):
                break
 
            # If vowel, then we insert it in hash
            hash[str1[j]] = 1
 
            # If all vowels are present in current
            # substr1ing
            if (len(hash) == 5):
                print(str1[i:j + 1], end = " ")
 
# Driver code
str1 = "aeoibsddaeiouudb"
FindSubstr1ing(str1)
 
# This code is contributed by Mohit Kumar


C#
// C# program to find all substring that
// contain all vowels
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Returns true if x is vowel.
public static bool isVowel(char x)
{
    // Function to check whether a
    // character is vowel or not
    return (x == 'a' || x == 'e' ||
            x == 'i' || x == 'o' || x == 'u');
}
 
public static void findSubstring(string str)
{
    HashSet hash = new HashSet();
     
    // To store vowels    
    // Outer loop picks starting character and
    // inner loop picks ending character.
    int n = str.Length;
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
 
            // If current character is not vowel,
            // then no more result substrings
            // possible starting from str[i].
            if (isVowel(str[j]) == false)
            {
                break;
            }
 
            // If vowel, then we insert it in hash
            hash.Add(str[j]);
 
            // If all vowels are present in current
            // substring
            if (hash.Count == 5)
            {
                Console.Write(str.Substring(i,     
                             (j + 1) - i) + " ");
            }
        }
        hash.Clear();
    }
}
 
// Driver code
public static void Main(string[] args)
{
    string str = "aeoibsddaeiouudb";
    findSubstring(str);
}
}
 
// This code is contributed by Shrikant13


Javascript


C++
// C++ program to find all substring that
// contain all vowels
#include
 
using namespace std;
 
// Returns true if x is vowel.
bool isVowel(char x)
{
    // Function to check whether a character is
    // vowel or not
    return (x == 'a' || x == 'e' || x == 'i' ||
                        x == 'o' || x == 'u');
}
 
// Function to FindSubstrings of string
void FindSubstring(string str)
{
    set hash;  // To store vowels
 
    int start = 0;
    for (int i=0; i


Java
// Java program to find all substring that 
// contain all vowels
import java.util.HashSet;
 
public class GFG {
 
    // Returns true if x is vowel.
    static boolean isVowel(char x) {
        // Function to check whether a character is
        // vowel or not
        return (x == 'a' || x == 'e' || x == 'i'
                || x == 'o' || x == 'u');
    }
 
    // Function to FindSubstrings of string
    static void findSubstring(String str) {
        HashSet hash = new HashSet();
        // To store vowels
 
        int start = 0;
        for (int i = 0; i < str.length(); i++) {
            // If current character is vowel then
            // insert into hash ,
            if (isVowel(str.charAt(i)) == true) {
                hash.add(str.charAt(i));
 
                // If all vowels are present in current
                // substring
                if (hash.size() == 5)
                    System.out.print(str.substring(start, i + 1) + " ");
            } else {
                start = i + 1;
                hash.clear();
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        String str = "aeoibsddaeiouudb";
        findSubstring(str);
    }
 
}


Python3
# Python3 program to find all substring
# that contain all vowels
 
# Returns true if x is vowel.
def isVowel(x):
     
    # Function to check whether
    # a character is vowel or not
    return (x == 'a' or x == 'e' or
            x == 'i' or x == 'o' or
            x == 'u');
 
# Function to FindSubstrings of string
def FindSubstring(str):
 
    hash = set(); # To store vowels
 
    start = 0;
    for i in range(len(str)):
         
        # If current character is vowel
        # then insert into hash
        if (isVowel(str[i]) == True):
            hash.add(str[i]);
 
            # If all vowels are present
            # in current substring
            if (len(hash) == 5):
                print(str[start : i + 1],
                              end = " ");
        else:
            start = i + 1;
            hash.clear();
 
# Driver Code
str = "aeoibsddaeiouudb";
FindSubstring(str);
 
# This code is contributed by 29AjayKumar


C#
using System;
using System.Collections.Generic;
 
// c# program to find all substring that  
// contain all vowels 
 
public class GFG
{
 
    // Returns true if x is vowel.
    public static bool isVowel(char x)
    {
        // Function to check whether a character is
        // vowel or not
        return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
    }
 
    // Function to FindSubstrings of string
    public static void findSubstring(string str)
    {
        HashSet hash = new HashSet();
        // To store vowels
 
        int start = 0;
        for (int i = 0; i < str.Length; i++)
        {
            // If current character is vowel then
            // insert into hash ,
            if (isVowel(str[i]) == true)
            {
                hash.Add(str[i]);
 
                // If all vowels are present in current
                // substring
                if (hash.Count == 5)
                {
                    Console.Write(str.Substring(start, (i + 1) - start) + " ");
                }
            }
            else
            {
                start = i + 1;
                hash.Clear();
            }
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string str = "aeoibsddaeiouudb";
        findSubstring(str);
    }
 
}
 
// This code is contributed by Shrikant13


Javascript


输出:

aeiou aeiouu

时间复杂度: O(n 2 )

优化方案:
对于每个字符,如果当前字符是元音,则插入到哈希中。否则将标志 Start 设置为从第 i+1 个索引开始的下一个子串。如果包含所有元音,则打印当前子字符串。

C++

// C++ program to find all substring that
// contain all vowels
#include
 
using namespace std;
 
// Returns true if x is vowel.
bool isVowel(char x)
{
    // Function to check whether a character is
    // vowel or not
    return (x == 'a' || x == 'e' || x == 'i' ||
                        x == 'o' || x == 'u');
}
 
// Function to FindSubstrings of string
void FindSubstring(string str)
{
    set hash;  // To store vowels
 
    int start = 0;
    for (int i=0; i

Java

// Java program to find all substring that 
// contain all vowels
import java.util.HashSet;
 
public class GFG {
 
    // Returns true if x is vowel.
    static boolean isVowel(char x) {
        // Function to check whether a character is
        // vowel or not
        return (x == 'a' || x == 'e' || x == 'i'
                || x == 'o' || x == 'u');
    }
 
    // Function to FindSubstrings of string
    static void findSubstring(String str) {
        HashSet hash = new HashSet();
        // To store vowels
 
        int start = 0;
        for (int i = 0; i < str.length(); i++) {
            // If current character is vowel then
            // insert into hash ,
            if (isVowel(str.charAt(i)) == true) {
                hash.add(str.charAt(i));
 
                // If all vowels are present in current
                // substring
                if (hash.size() == 5)
                    System.out.print(str.substring(start, i + 1) + " ");
            } else {
                start = i + 1;
                hash.clear();
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        String str = "aeoibsddaeiouudb";
        findSubstring(str);
    }
 
}

蟒蛇3

# Python3 program to find all substring
# that contain all vowels
 
# Returns true if x is vowel.
def isVowel(x):
     
    # Function to check whether
    # a character is vowel or not
    return (x == 'a' or x == 'e' or
            x == 'i' or x == 'o' or
            x == 'u');
 
# Function to FindSubstrings of string
def FindSubstring(str):
 
    hash = set(); # To store vowels
 
    start = 0;
    for i in range(len(str)):
         
        # If current character is vowel
        # then insert into hash
        if (isVowel(str[i]) == True):
            hash.add(str[i]);
 
            # If all vowels are present
            # in current substring
            if (len(hash) == 5):
                print(str[start : i + 1],
                              end = " ");
        else:
            start = i + 1;
            hash.clear();
 
# Driver Code
str = "aeoibsddaeiouudb";
FindSubstring(str);
 
# This code is contributed by 29AjayKumar

C#

using System;
using System.Collections.Generic;
 
// c# program to find all substring that  
// contain all vowels 
 
public class GFG
{
 
    // Returns true if x is vowel.
    public static bool isVowel(char x)
    {
        // Function to check whether a character is
        // vowel or not
        return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
    }
 
    // Function to FindSubstrings of string
    public static void findSubstring(string str)
    {
        HashSet hash = new HashSet();
        // To store vowels
 
        int start = 0;
        for (int i = 0; i < str.Length; i++)
        {
            // If current character is vowel then
            // insert into hash ,
            if (isVowel(str[i]) == true)
            {
                hash.Add(str[i]);
 
                // If all vowels are present in current
                // substring
                if (hash.Count == 5)
                {
                    Console.Write(str.Substring(start, (i + 1) - start) + " ");
                }
            }
            else
            {
                start = i + 1;
                hash.Clear();
            }
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string str = "aeoibsddaeiouudb";
        findSubstring(str);
    }
 
}
 
// This code is contributed by Shrikant13

Javascript


输出:

aeiou aeiouu

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程