HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Find if the string contains ALL the vowels

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theallvowelscontainsfindstring

Problem

Problem:


You are given a randoms string containing only lowercase letters
and you need to find if the string contains ALL the vowels.

Input:


First line contains N , the size of the string.
Second line contains the letters (only lowercase).

Output:


Print "YES" (without the quotes) if all vowels are found in the string, "NO" (without the quotes) otherwise.

Constraints:


The size of the string will not be greater than 10,000
1 ≤ N ≤ 10000

The above problem is from hackerearth.com and below is my attempt to solve this problem and I need your review on my codestyle, and efficiency and need your help to make it better.

public class TestClass {

public static void main(String[] args) throws IOException {

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the Length of the String");
    final int inputLength = Integer.parseInt(input.readLine());//the size of the string

    if (inputLength >= 10000) {
        System.err.println("The size of the string can not be greater than 10,000");
        System.exit(1);
    }

    System.out.println("Enter letters");
    String word = input.readLine().toLowerCase().trim();

    //remove all white space
    word = word.replaceAll(" ", "");

    final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    short found = 0;

    for (final char c : vowels) if (searchForVowel(c, word)) found++;

    String result = (found == vowels.length) ? "yes" : "no";

    System.out.println(result);
}

private static boolean searchForVowel(char vowel, String word) {
    final char[] chars = word.toCharArray();
    boolean isContains = false;

    for (char c : chars) {
        if (c == vowel) {
            isContains = true;
            break;
        }
    }

    return isContains;
  }

}

Solution

I would like to make one remark concerning the performance of your solution: you need to check if the word contains all vowels ('a', 'e', 'i', 'o' and 'u'), which means the following:

  • If the word does not contain 'a', then it's not good, else:



  • If the word does not contain 'e' (we already know it contains 'a'), it's not good, else:



  • ...



But: in the English language, some vowels are more popular than others. Imagine following percentages (not real ones):

60% of the English words contain an 'a'

80% of the English words contain an 'e'

40% of the English words contain an 'i'

20% of the English words contain an 'o'

10% of the English words contain an 'u'

If you start searching with 'a', then with 'e', then with 'i', 'o', and 'u', you have following situation:

40% probability that you have found out that the word is not good, then:

20% probability that you have found out that the word is not good, then:

60% probability that you have found out that the word is not good, then:

80% probability that you have found out that the word is not good, then:

90% probability that you have found out that the word is not good.

=> in other words, you'll spend a lot of time investigating letters which are not popular.

However, if you re-order your search criteria:

  • first search for 'u'



  • then search for 'o'



  • then search for 'i'



  • then search for 'a'



  • and only finally search for 'e'




=> like this, you will find much sooner the words without 'u' (90% of the words), or the ones without an 'o' (80% of the words), ..., and this might heavily improve the performance of your search.

I must add that the percentages I have written down are just examples (for the correct percentages you need to search on frequency analysis of the English language).

Good luck

Context

StackExchange Code Review Q#112575, answer score: 16

Revisions (0)

No revisions yet.