繁体   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

我已经成功地计算了字符串数组中每个元素的元音数量。 但是我无法比较它们并打印出元音数量最少的数组元素。 请帮帮我。 这是我到目前为止编写的代码。

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;
    }
}

我已经修改了您的代码。请尝试运行以下代码

  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

}

创建一个适合您的自定义编译器类,如下所示:

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);
    }
}

然后使用上面的编译器通过使用另一个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]);
}

您可以使用正则表达式,而不是比较各个字符以获得元音计数

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

您还可以使用流进行排序。

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);

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

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