Python - 将字符串中的重复字符大写
给定一个带有小写字母的输入字符串,任务是编写一个Python程序来识别字符串中重复的字符并将它们大写。
例子:
Input: programming language
Output: pRoGRAMMiNG lANGuAGe
Explanation: r,m,n,a,g are repeated elements
Input: geeks for geeks
Output: GEEKS for GEEKS
Explanation: g,e,k,s are repeated elements
方法:
- 我们必须将字符串字符键,将字符串中每个字符出现的频率作为字典中的值。
- 遍历字符串并使用字典检查每个字符的频率,如果字符的频率大于 1,则使用 upper()函数将字符更改为大写。
执行:
Python3
# function for changing the
# repeated characters to uppercase
def RepeatedUpper(s):
# declaring dictionary
dic = {}
# Traversing the string
for i in s:
# If the character is already
# present in dictionary then increment
# the frequency of the character
if i in dic:
dic[i] = dic[i]+1
# If the character is not present in
# the dictionary then inserting
# the character in the dictionary
else:
dic[i] = 1
ans = ''
# traversing the string
for i in s:
# if the frequency of the character is
# greater than one
if dic[i] > 1:
# change into uppercase
i = i.upper()
# appending each character to the ans
ans = ans+i
return ans
# Driver code
s = 'geeks for geeks'
# fuction call
print(RepeatedUpper(s))
输出:
GEEKS for GEEKS
时间复杂度: O(n)
空间复杂度: O(n)