字符串匹配相关

目录

题目描述:

输入描述:

输出描述:

我自己的代码(通过了所有用例)


题目描述:

给出一组字符串,找出其中以小写的d或e字母结尾的单词出现的次数,结果以字典格式打印。其中不以d或e结尾的单词,不在结果统计中出现。

输入描述:

若干个字符串,它们以逗号进行分隔,样例如下所示:

abc,head,tail,middle,head,today,tail,rain,end,cup,apple,abc,head,tail,middle,head,today,tail,rain,end,cup,apple,abc,head,tail,middle,head,today,tail,rain,end,cup,apple,abc,head,tail,middle,head,today,tail,rain,end,cup,apple,abc,head,tail,middle,head,today,tail,rain,end,cup,apple,hard,abc,head,tail,middle,head,today,tail,rain,end,cup,apple

输出描述:

以字典格式打印,样例输出如下所示:

{head=12, apple=6, middle=6, end=6, hard=1}

我自己的代码(通过了所有用例)

import java.util.*;
import java.io.*;

public class One {

	public static void main(String[] args) throws Exception {
		BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
		
		String [] readStr = bfr.readLine().split(",");
		
		HashMap keyValue = new HashMap();
		
		int readStrLen = readStr.length;
		for (int i = 0; i < readStrLen; i++) {
			if(readStr[i].contains("d") || readStr[i].contains("e")) {
				String[] temStrArr = readStr[i].split("");
				int strLen = temStrArr.length;
				
				if(temStrArr[strLen - 1].equals("d") || temStrArr[strLen - 1].equals("e")) {
					if(keyValue.containsKey(readStr[i])) {
						keyValue.put(readStr[i], keyValue.get(readStr[i]) + 1);
					}else {
						keyValue.put(readStr[i], 1);
					}
				}
			}
			
		}
		
		/*====================输出打印====================*/
		System.out.print("{");
		int tem = 0;
		for(String i : keyValue.keySet()) {
			System.out.print(i + "=" + keyValue.get(i));
			tem++;
			if(tem <= keyValue.size() - 1) {
				System.out.print(", ");
			}else {
				System.out.println("}");
			}
		}

	}

}

 

你可能感兴趣的:(互联网笔试算法题)