题目
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
输入描述:
每个测试输入包含2个字符串
输出描述:
输出删除后的字符串
代码
import java.util.*;
public class Main{
public static void main (String[]args){
Scanner scan = new Scanner(System.in);
String str1 = scan.nextLine();
String str2 = scan.nextLine();
HashMap<Character,Integer> map = new HashMap<>();
//1.遍历第二个字符串
for(int i = 0;i < str2.length();i++){
//当前拿到的字符判断,之前是否存在于map中
if(map.get(str2.charAt(i)) == null){ //
map.put(str2.charAt(i),1); // key ,value
}else{
map.put(str2.charAt(i),map.get(str2.charAt(i))+1);
}
}
//2.遍历第一个字符串
String ret = "";
for(int i = 0;i < str1.length();i++){
if(map.get(str1.charAt(i)) == null){
ret += str1.charAt(i);
}
}
System.out.println(ret);
}
}
`