题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
该题不难,就是有点繁琐,涉及到字段 字符串和数组的运用,代码如下:
def strdelete(str1):
dic1={}
for i in str1:
if i in dic1:
dic1[i]+=1
else:
dic1[i]=1
# print(dic1)
list1=sorted(dic1.items(),key=lambda x:x[1])
# print('list1',list1)
j=0
for i in range(len(list1)):
if list1[i][1]== list1[0][1]:
j=i
# print('j',j)
list2=[]
for i in range(j+1):
list2.append(list1[i][0])
# print('list2',list2)
list3=[]
for i in str1:
if i not in list2:
list3.append(i)
return ''.join(list3)
list4=[]
while True:
try:
s=input()
list4.append(strdelete(s))
except:
break
for i in list4:
print(i)
dic1={}
for i in str1:
if i in dic1:
dic1[i]+=1
else:
dic1[i]=1
# print(dic1)
list1=sorted(dic1.items(),key=lambda x:x[1])
# print('list1',list1)
j=0
for i in range(len(list1)):
if list1[i][1]== list1[0][1]:
j=i
# print('j',j)
list2=[]
for i in range(j+1):
list2.append(list1[i][0])
# print('list2',list2)
list3=[]
for i in str1:
if i not in list2:
list3.append(i)
return ''.join(list3)
list4=[]
while True:
try:
s=input()
list4.append(strdelete(s))
except:
break
for i in list4:
print(i)