本题显然想到用hashmap,ascii字符作为key的HashMap都可以用int[128]代替
两次遍历即可,本题注意每一行输入数据的处理即可
import java.util.*; public class Main { public static String indexOfFirstK(String s, int k){ int[] ascii = new int[128]; int cnt = k; for(int i = 0; i < s.length(); i++){ ascii[s.charAt(i)]++; } for(int i = 0; i < s.length(); i++){ if(ascii[s.charAt(i)] == 1){ cnt--; if(cnt == 0){ return "[" + s.charAt(i) + "]"; } } } return "Myon~"; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String s = scanner.nextLine(); int k = Integer.parseInt(s.split(" ")[0]); s = s.substring(s.split(" ")[0].length() + 1); System.out.println(indexOfFirstK(s, k)); } } }