题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.;
import java.io.;
public class Main{
public static String delete(String str){
Map<Character,Integer> map = new HashMap<>();
//将每个字母和key对应
for(char ch : str.toCharArray()){
map.put(ch,map.getOrDefault(ch,0) + 1);
}
//找到出现次数key
StringBuilder s = new StringBuilder();
int min = Integer.MAX_VALUE;
for(int index : map.values()){
min = Math.min(min,index);
}
//删除出现次数最少的key的value
for(char ch : str.toCharArray()){
if(map.get(ch) != min){
s.append(ch);
}
}
return s.toString();
//输出最后的字符串
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String string = delete(str);
System.out.println(string);
}
}