简体   繁体   English

给定数字N和N个字符串数组,请基于每个字符串中的元音数量以降序对字符串进行排序

[英]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: 然后使用上面的编译器通过使用另一个Arrays.sort()签名对数组进行排序,该签名将Arrays.sort()器作为第二个参数:

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 这依赖于拥有长度和值的Pair类

private class Pair {

    int length;
    String value;

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定数字n作为输入,返回长度为n的新字符串数组,其中包含字符串“ 0”,“ 1”,“ 2”,依此类推,直到n-1 - Given a number n as input, return a new string array of length n, containing the strings “0”, “1”, “2” so on till n-1 编写一个方法,该方法采用字符串数组,并按从最小到最大的元音数量对字符串进行排序,并保持字母顺序 - writing a method that takes an array of strings and orders the strings by number of vowels from least to greatest, and keeps the alphabetical order 如何按每个字符串具有的元音数量按降序对字符串数组进行排序 - How to sort an array of string in descending order by the number of vowels each string has 按降序对字符串进行冒泡排序 - Bubble sort strings in descending order 排序包含数字的字符串 - Sort strings that contains number 根据长度升序对字符串数组进行排序 - Sort an array of strings based on length in ascending order 在Java中按单词的索引号对字符串进行排序(数字作为参数给出) - Sort strings by index number of word (the number is given as an argument) in java 在某些字符串的名称上添加+ n以“ v +数字”结尾 - add +n to the name of some Strings id ends with “v + number” 如何将N个字符串编码为具有最小位数的二进制数 - How to encode N strings to binary numbers with minimum number of digits 在Java中,如何创建n个具有某些属性的字符串 - in Java, how to create n number of Strings with certain properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM