题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hll[yellowdradra@foxmail.com]
 * @since 2023-03-10 15:21
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        final Map<String, Integer> map = new HashMap<>(str.length());
        Arrays.stream(str.toLowerCase().split("")).map(c -> map.compute(c, (k, v) -> v == null ? 1 : ++v)).collect(Collectors.toList());
        System.out.println(map.getOrDefault(in.next().toLowerCase(), 0));
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务