当前位置:网站首页>牛客:删除公共字符
牛客:删除公共字符
2022-07-30 05:48:00 【HDLaZy】
题目描述:
解题思路:
使用Java的HashMap集合框架,HashMap无重复值,将字符串2的每一个字符存入其中,然后遍历字符串1的每一个字符,判断map中是否存在该值,如果不存在则拼接到新的字符串中,如果存在则不进行拼接。
代码:
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<> ();
for(int i=0; i< str2.length() ;i++){
if( map.get( str2.charAt(i))== null){
map.put(str2.charAt(i),1);
}else{
map.put(str2.charAt(i),map.get(str2.charAt(i))+1);
}
}
String result="";
for(int i=0; i< str1.length() ; i++){
if(map.get(str1.charAt(i))==null){
result+=str1.charAt(i);
}
}
System.out.println(result);
}
}
边栏推荐
猜你喜欢
四、6、前缀、中缀、后缀表达式(逆波兰表达式)
事件传递和响应者链条
Biotin-C6-amine_N-生物素基-1,6-己二胺_CAS:65953-56-2_100mg
Unity Gizmos扩展:线框圆
图扑软件携手华为云再创合作共赢新局面
flask项目快速搭建部署gunicorn+supervisor
IO进程线程->标准IO->day1
Biotin-Dadps-azide|CAS:1260247-50-4|生物素-DADPS-叠氮
DADPS Biotin Azide( CAS:1260247-50-4生物素基团和叠氮基团的 PEG 衍生物
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
随机推荐
基于粒子(Points)模拟雨雪天气效果
掌握JESD204B(三)–AD6676的调试
Biotin-PEG4-DADPS-Picolyl-azide(CAS:2599839-59-3)生物素试剂
基于 JupyterLab 插件在 GraphScope 中交互式构图
测试第一题
进制详解(二进制、八进制、十进制、十六进制详解及相互转换,位运算)
测试第二题
Mastering JESD204B (1) – Debugging of AD6676
Vim find character
不依赖框架的文件下载
instantDrag for Maya脚本 (移动模型时沿目标模型移动)
单片机第一步
THREEJS辉光与景深特效
IO进程线程->文件IO->day2
ParseException line 8:13 mismatched input ‘(‘ expecting ) near ‘int‘ in create table statement
爬楼梯C语言
OP tokens and non-transferable NFTs work to build a new digital democracy
陕西Biotin-LC_CAS:72040-64-3_N-生物素氨基己酸供应商价格
Devops基本概念和原理
如何使用xilinx的FFT ip