实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
数据范围:输入的字符串长度满足 ,保证输入的字符串中仅出现小写字母
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。
删除字符串中出现次数最少的字符后的字符串。
aabcddd
aaddd
get_str = list(input()) # 哈希表 key:字符 val:出现次数 hashtable = dict() for i in get_str: if i in hashtable.keys(): hashtable[i] += 1 else: hashtable[i] = 1 lst_key = list(hashtable.keys()) lst_val = list(hashtable.values()) n = len(lst_key) min_str = [] min_num = min(lst_val) for i in range(0, n): if lst_val[i] == min_num: min_str.append(lst_key[i]) for i in range(min_num): for str_ in min_str: get_str.remove(str_) print(''.join(get_str))
import collections s = input() s_dict, res, minKeyLst = collections.Counter(), "", [] for c in s: s_dict[c] += 1 minValue=min(s_dict.values()) for k,v in s_dict.items(): if v==minValue: minKeyLst.append(k) for c in s: if c not in minKeyLst: res += c print(res)
int cnt[26] ={0}; void solution(const string &str) { for(auto i : str) { ++cnt[i - 'a']; } int minCnt = 100; for(int i = 0 ;i < 26; ++i) { if(cnt[i] && cnt[i] < minCnt) minCnt = cnt[i]; } for(auto i : str) { if(cnt[i - 'a'] != minCnt) cout <<i; } cout << endl; } int main(){ string str; while(cin >> str) { solution(str); } }问就是简单粗暴
#include<iostream> #include<string> #include<vector> using namespace std; int main() { string data; cin >> data; vector<int> ret(26); for (int index = 0; index < data.size(); index++) {//统计各个字符出现的次数 ret[data[index] - 'a']++; } int min = data[0] -'a'; for (int index = 0; index < 26 ; index++) {//找出出现次数最少的字符 if (ret[index] != 0 && ret[min] > ret[index]) { min = index; } } //出现次数最少的次数是ret[min] for(int i = 0 ; i < data.size() ; i++){ if(ret[data[i] - 'a'] != ret[min]){ cout<<data[i]; } } cout<<endl; return 0; }
st = input() st_lst = list(st) st_set = set(st_lst) st_dic ={} def dic_str(): for j in range(97,123): count = str(st.count(chr(j))) if count not in st_dic.keys() : st_dic[count] = [] if chr(j) in st_set: if len(st_dic[count]) == 0 : st_dic[count] = chr(j) else: st_dic[count] = st_dic[count] + chr(j) del st_dic['0'] lst = sorted(list(st_dic.keys())) st_str = st_dic[lst[0]] return st_str st_str = dic_str() st_lst1 = st_lst.copy() for a in range(len(st_lst1)): if st_lst1[a] in st_str: st_lst.remove(st_lst1[a]) print(''.join(st_lst))
# coding: utf-8 def func(strs): detail = {} s ='' for i in sorted(strs): count_cur = strs.count(i) detail[i] = count_cur count_min = min(detail.values()) for k in detail.keys(): if detail[k] == count_min: strs = strs.replace(k,"") print strs if __name__ == "__main__": import sys strs = sys.stdin.readline().strip() func(strs)
#include<stdio.h> #include<string.h> int main() { char arr[21]={0}; int num[124]={0},i=0; gets(arr); while(i<strlen(arr)) { num[arr[i++]]++; } int min=20; for(int i=0;i<124;i++) { if(num[i]!=0&&num[i]<min) min=num[i]; } for(int i=0;i<strlen(arr);i++) { if(num[arr[i]]!=min) printf("%c",arr[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; map<char, int> hashmap; for (auto c : str) { hashmap[c]++; // 记录每个字符出现的次数 } int times = INT_MAX; for (auto i : hashmap) { times = min(times, i.second); // 找到最少次数 } for (auto c: str) { if (hashmap[c] > times) { // 找到出现次数大于最小次数的字符,并依次输出 cout << c; } } cout << endl; return 0; }
#include<stdio.h> int main() { char str[21]={0}; int i; while(EOF!=scanf("%s",str)) { char cnt[26]={0}; int min = 20; int len = 0; for(i=0;str[i]!=0;i++)cnt[str[i]-'a']++; len = i; for(i=0;i<26;i++)if(cnt[i]>0 && min>cnt[i])min=cnt[i]; for(i=0;i<len;i++)if(min!=cnt[str[i]-'a'])printf("%c",str[i]); printf("\n"); } return 0; }
// 2021.11.11 // 不熟,要多练习!!! import java.util.*; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); while (scan.hasNext()){ String str = scan.nextLine(); Map<Character, Integer> map = new HashMap<Character, Integer>(); for (char c:str.toCharArray()){ if (map.containsKey(c)){ map.put(c,map.get(c)+1); }else{ map.put(c,1); } } // 在JDK中,整形类型是有范围的,最大值为Integer.MAX_VALUE,即2147483647, // 最小值为Integer.MIN_VALUE -2147483648 // 遍历得到字符出现最小的次数 int min = Integer.MAX_VALUE; for(Map.Entry<Character, Integer> entry:map.entrySet()){ if (entry.getValue() < min){ min = entry.getValue(); } } // 删除字符 for (Map.Entry<Character, Integer> entry:map.entrySet()){ if (entry.getValue() == min){ str = str.replace(entry.getKey() + "", ""); } } System.out.println(str); } } }
from collections import Counter def test(aa): bb = [] for i in aa: # 将字符串打散,放入列表中 bb.append(i) # 统计每个字符串个数 cc = Counter(bb) dd = [] for j in bb: # 将cc转成列表 dd.append([j, cc[j]]) for i1 in range(len(dd)): # 将列表相同的字段替换掉 for i2 in range(i1 + 1, len(dd)): if dd[i1] == dd[i2]: dd[i2] = 'f' # 列表去重 while 'f' in dd: dd.remove('f') qqqq = [] for i3 in range(len(dd)): # 将去重后的数据存入列表 qqqq.append(dd[i3][1]) # 查询列表最小值 value = min(qqqq) ww = [] for k2 in dd: # 将列表中最小值对应的字母找出来 if k2[1] == value: ww.append(k2) for k1 in ww: # 将列表中最小值对应的字母删除 while k1[0] in bb: bb.remove(k1[0]) ee = '' for l in bb: ee += l print(ee) while True: try: test(input()) except: break
let line while(line = readline()) { let arr = [...new Set(line.split(''))] let letter = [] for(let item of arr) { let index = line.split('').filter(e => e === item).length letter[index] ? letter[index].push(item) : letter[index] = [item] } letter = letter.filter(() => true) let reg = new RegExp(`[${letter[0].join('')}]`, 'g') console.log(line.replace(reg, '')) }
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while((str = br.readLine())!=null){ HashMap<Character,Integer> mp = new HashMap<>(); int len = str.length(); for(int i =0;i<len;i++){ mp.put(str.charAt(i),mp.getOrDefault(str.charAt(i),0)+1); } int m = Integer.MAX_VALUE; for(Map.Entry<Character,Integer> entry: mp.entrySet()){ m = Math.min(m,entry.getValue()); } int i = 0; for(;i<len;i++){ if(mp.get(str.charAt(i))==m){ continue; } System.out.print(str.charAt(i)); } System.out.println(); } } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.next(); char[] arr = str.toCharArray(); Map<Character,Integer> map = new LinkedHashMap(); for(int i=0;i<arr.length;i++){ if(map.keySet().contains(arr[i])){ map.put(arr[i],map.get(arr[i])+1); }else{ map.put(arr[i],1); } } int min = arr.length; for(Integer i:map.values()){ if(i<min){ min = i; } } for(int i=0;i<arr.length;i++){ if(map.get(arr[i])!=min){ System.out.print(arr[i]); } } System.out.println(); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); List<String> list = new ArrayList<String>(); while(input.hasNext()){ list.add(input.next()); } for(int i=0; i<list.size(); i++){ String word = list.get(i); Map<Character,Integer> map = count(word); //print(map); //find the minimum occurrence int minOcc = map.get(word.charAt(0)); for(int j=0; j<word.length(); j++){ if(map.get(word.charAt(j))<=minOcc){ minOcc = map.get(word.charAt(j)); } } //System.out.println("minOcc="+minOcc); //print the new string for(int j=0; j<word.length(); j++){ if(map.get(word.charAt(j))>minOcc){ System.out.print(word.charAt(j)); } } System.out.println(); } } public static Map count(String word){ Map<Character,Integer> map = new HashMap<>(); for(int i=0; i<word.length(); i++){ char ch = word.charAt(i); if(map.containsKey(ch)){ int num = map.get(ch); map.put(ch,++num); }else{ map.put(ch,1); } } return map; } public static void print(Map<Character,Integer> map){ for(Character keys: map.keySet()){ System.out.println("key="+keys+",value="+map.get(keys)); } } }