题解 | #计算某字母出现次数#
计算某字母出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine().trim(); Map<Character, Integer> map = new HashMap<>(); for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); if(c >= 'a'&& c <= 'z'){ if(map.containsKey(c)){ map.put(c, (map.get(c) + 1)); }else{ map.put(c, 1); } }else if(c >= 'A' && c <= 'Z'){ c = Character.toLowerCase(c); if(map.containsKey(c)){ map.put(c, (map.get(c) + 1)); }else{ map.put(c, 1); } }else{ continue; } } String charStr = br.readLine(); char target = Character.toLowerCase(charStr.charAt(0)); if(map.containsKey(target)){ System.out.print(map.get(target)); }else{ System.out.println(0); } } }