简体   繁体   中英

Given a number N and an array of N strings, sort the strings based on number of vowels in each of the strings in the descending order

I had successfully counted the number of vowels in each element of string array. But i am unable to compare them and the print the array element which has the least number of vowels. Please help me out. This is the code i had written so far....

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int N = s.nextInt();
    int vowelcount = 0;
    int maxcount = 0, sum = 0;
    String a[] = new String[N];

    for(int i = 0; i < N; i++) {
        a[i] = s.next();
    }

    for(int i = 0; i < N; i++) {
        String str = a[i];
        for(int j = 0; j < str.length(); j++) {
            if(str.charAt(j) == 'a' || str.charAt(j) == 'e' || str.charAt(j) == 'i'
                    || str.charAt(j) == 'o' || str.charAt(j) == 'u') {
                vowelcount = vowelcount + 1;
            }
        }

        System.out.println(vowelcount);
        vowelcount = 0;
    }
}

I have modified your code.. Please try running the below code

  public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    int vowelcount=0;
    int maxcount=0,sum=0;
    String a[]=new String[N];
    for(int i=0;i<N;i++){
        a[i]=s.next();
    }
    //added
    int minCount = Integer.MAX_VALUE;
    int minCountIndex = Integer.MAX_VALUE;
    //till here
    for(int i=0;i<N;i++){
        String str=a[i];
        for(int j=0;j<str.length();j++){
            if(str.charAt(j)=='a'||str.charAt(j)=='e'||str.charAt(j)=='i'||str.charAt(j)=='o'||str.charAt(j)=='u'){
                vowelcount=vowelcount+1;

            }

        } //Add below lines
        if(vowelcount < minCount) {
            minCount = vovelcount;
            minCountIndex = i;
        } //till here
        System.out.println(vowelcount);
        vowelcount=0;
    }
    System.out.println("String with Minimum Vovels :" + a[minCountIndex]);  // Added this

}

Create a custom comperator class which suits your needs like this:

static class SortDescendingByNumberOfVowels implements Comparator<String> {
    private int getNumberOfVowels(String str) {
        int counter = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
                counter += 1;
            }
        }
        return counter;
    }

    @Override
    public int compare(String str1, String str2) {
        return getNumberOfVowels(str2) - getNumberOfVowels(str1);
    }
}

Then use the above comperator to sort the array by using another signature of Arrays.sort() which takes as a 2nd parameter the comperator:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int N = Integer.parseInt(s.nextLine());
    String a[] = new String[N];

    for(int i = 0; i < N; i++) {
        a[i] = s.nextLine();
    }

    System.out.println("Initial array: " + Arrays.toString(a));

    Arrays.sort(a, new SortDescendingByNumberOfVowels());
    System.out.println("Sorted array : " + Arrays.toString(a));

    System.out.println("Item with less vowels: " + a[N - 1]);
}

You could use a regex rather than comparing individual characters to get the vowel count

Pattern vowels = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);
int numberOfVowels = value.length() - vowels.matcher(value).replaceAll("").length();

You could also use a stream to do the sorting.

Pair fewest = Arrays.stream(values)
        .map(value -> new Pair(value.length() - vowels.matcher(value).replaceAll("").length(), value))
        .sorted((v1, v2) -> Integer.compare(v1.length, v2.length))
        .findFirst().orElse(null);

System.out.println(fewest.value);

That relies on having a Pair class which holds the length and value

private class Pair {

    int length;
    String value;

    Pair(int length, String value) {
        this.length = length;
        this.value = value;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM