四、+输入+M+个字符串,+输出重复+1+到+M+次的字符串的个数。+例如:+输入+BBA+BBA+BEA+DEC+CCF+输出+1+3;+2+1;+3+0;+4+0;+5+0;
时间: 2023-10-27 22:08:20 浏览: 117
首先,我们可以使用字典来记录每个字符串出现的次数。然后,我们遍历字典中的值,计算每个重复次数对应的字符串个数。
以下是实现该功能的Python代码:
```python
def count_string_duplicates(strings):
counts = {}
for string in strings:
if string in counts:
counts[string] += 1
else:
counts[string] = 1
duplicates = {}
for count in counts.values():
if count in duplicates:
duplicates[count] += 1
else:
duplicates[count] = 1
return duplicates
strings = ['BBA', 'BBA', 'BEA', 'DEC', 'CCF']
result = count_string_duplicates(strings)
for i in range(1, len(strings) + 1):
if i in result:
print(i, result[i])
else:
print(i, 0)
```
输出结果为:
```
1 3
2 1
3 0
4 0
5 0
```
阅读全文