实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
数据范围:输入的字符串长度满足 ,保证输入的字符串中仅出现小写字母
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。
删除字符串中出现次数最少的字符后的字符串。
aabcddd
aaddd
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); String result = reChars(input); System.out.println(result); } public static String reChars(String str) { // 使用HashMap来存储每个字符出现的次数 Map<Character, Integer> charCountMap = new HashMap<>(); for (char c : str.toCharArray()) { charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1); } // 找出出现次数最少的字符 int minCount = Integer.MAX_VALUE; for (int count : charCountMap.values()) { if (count < minCount) { minCount = count; } } // 删除出现次数最少的字符 StringBuilder result = new StringBuilder(); for (char c : str.toCharArray()) { if (charCountMap.get(c) > minCount) { result.append(c); } } return result.toString(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int[] ss = new int[26]; String str = in.nextLine(); for(int i=0;i<str.length();i++){ ss[str.charAt(i)-'a']++; } int minCount = 21; for(int i=0;i<26;i++) { if(ss[i] > 0 && ss[i]< minCount) { minCount = ss[i]; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { if(ss[str.charAt(i) - 'a'] != minCount) { sb.append(str.charAt(i)); } } System.out.println(sb); } } }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { String str = input.nextLine(); HashMap<Character, Integer> count = new HashMap<>(); for (char c : str.toCharArray()) { count.put(c, count.getOrDefault(c, 0) + 1); } //找出出现次数最少的字符的次数 int min = Integer.MAX_VALUE; for (int num : count.values()) { min = Math.min(min, num); } //构建新的字符串,去掉出现次数最少的字符 StringBuilder sb = new StringBuilder(); for (char c : str.toCharArray()) { if (count.get(c) > min) { sb.append(c); } } System.out.println(sb.toString()); } } }
import java.util.HashMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); HashMap<Character,Integer> map = new HashMap<>(); for(int i=0;i<input.length();i++) { char tmp = input.charAt(i); if(map.containsKey(tmp)) { map.replace(tmp, map.get(tmp)+1); } else { map.put(tmp,1); } } int minValue = Integer.MAX_VALUE; for(Integer value:map.values()) { if(value<minValue) { minValue = value; } } for(int j=0;j<input.length();j++) { char tmp = input.charAt(j); if(map.get(tmp)!=minValue) { System.out.print(tmp); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); //统计所有字符的个数 HashMap<Character, Integer> map = new HashMap(); for (int i = 0; i < str.length(); i++) { char a = str.charAt(i); if (map.containsKey(a)) { int count = map.get(a); map.put(a, ++count); } else { map.put(a, 1); } } StringBuilder sb = new StringBuilder(""); //找到最小值并删除 int min = Collections.min(map.values()); Iterator <Map.Entry<Character, Integer>>it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Character, Integer> entry = it.next(); if (entry.getValue() == min) { it.remove(); } } //map里面存在的字符都是符合条件的,对比字符串,不断拼接符合条件爱你的字符 for(int i =0;i<str.length();i++){ if(map.containsKey(str.charAt(i))){ sb.append(str.charAt(i)); } } System.out.println(sb.toString()); } }
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { HashMap map = new HashMap(); String s = scan.next(); char[] chars = s.toCharArray(); for (char c : chars) { if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } Integer less = map.entrySet().stream() .sorted(Comparator.comparingInt(Map.Entry::getValue)).findFirst().get().getValue(); //获取出现次数最少的字符 List lessList = map.entrySet().stream() .filter(e -> e.getValue() == less).map(Map.Entry::getKey).collect(Collectors.toList()); StringBuilder sb = new StringBuilder(); for (char nc : chars) { if (!lessList.contains(nc)) sb.append(nc); } System.out.println(sb); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.next(); Map<Character, Integer> map = new HashMap(); char ch; for (int i = 0; i < str.length(); ++ i ) { ch = str.charAt(i); int t = map.getOrDefault(ch, 0); ++ t; map.put(ch, t); } Set<Character> set = map.keySet(); int min = 21; for (Character c : set) { if (map.get(c) < min) min = map.get(c); } for (Character c : set) { if (map.get(c) == min) { str = str.replace(c + "", ""); } } System.out.println(str); } }
import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); Map<Character, Integer> map = new HashMap<>(); for (char c: str.toCharArray()) { map.put(c, (map.getOrDefault(c, 0) + 1)); } Collection<Integer> values = map.values(); int min = Collections.min(values); for(Character c:map.keySet()){ if(map.get(c) == min){ str = str.replaceAll(String.valueOf(c),""); } } System.out.println(str); } }
import java.util.Scanner; import java.util.HashMap; import java.util.Map; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static String removeLeastFrequentChars(String input) { if (input == null || input.isEmpty()) { return input; } // 使用哈希表来统计字符出现的次数 Map<Character, Integer> charCount = new HashMap<>(); for (char c : input.toCharArray()) { charCount.put(c, charCount.getOrDefault(c, 0) + 1); } // 找到最少出现次数 int minCount = Integer.MAX_VALUE; for (int count : charCount.values()) { if (count < minCount) { minCount = count; } } // 构建结果字符串,删除出现次数最少的字符 StringBuilder result = new StringBuilder(); for (char c : input.toCharArray()) { if (charCount.get(c) != minCount) { result.append(c); } } return result.toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String result = removeLeastFrequentChars(input); System.out.println(result); } }
import java.util.HashSet; import java.util.Scanner; import java.util.Set; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int size = str.length(); int count = str.length(); Set<String> set = new HashSet<>(); for (int i = 0; i < str.length(); i++) { String s = str.charAt(i)+""; String st = str.replaceAll(s, ""); if (count > (size-st.length())) { count = size-st.length(); set.clear(); set.add(s); } if (count == (size-st.length()) && !set.contains(s)) { set.add(s); } } if (!set.isEmpty()) { for (String s:set) { str = str.replace(s, ""); } } System.out.println(str); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str=in.nextLine(); int[] arr=new int[str.length()]; for(int i=0;i<str.length();i++){ arr[i]=0; } for(int i=0;i<str.length();i++){ for(int j=0;j<str.length();j++){ if(str.charAt(i)==str.charAt(j)){ arr[i]=arr[i]+1; } } } int num=arr[0]; for(int i=1;i<str.length();i++){ if(num>arr[i]){ num=arr[i]; } } for(int i=0;i<str.length();i++){ if(arr[i]!=num){ System.out.print(str.charAt(i)); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.next(); deleteCharacter(str); } scanner.close(); } public static void deleteCharacter(String str) { // 统计字符频率 Map<Character, Integer> count = new HashMap<>(); char[] chs = str.toCharArray(); for (int i = 0; i < chs.length; i++) { Character ch = chs[i]; int num = count.get(ch) == null ? 0 : count.get(ch); count.put(ch, num + 1); } // 最小次数最大值应该是字符串长度,而不是Integer.MAX_VALUE或者21 int minCount = str.length(); for (Map.Entry<Character, Integer> entry : count.entrySet()) { if (entry.getValue() < minCount) minCount = entry.getValue(); } // 结果处理这里没有直接得到最终字符串,而是按照要求打印了字符串 for (int i = 0; i < chs.length; i++) { if (minCount != count.get(chs[i])) System.out.print(chs[i]); } System.out.println(); } }