替换字符串中的字符为“(” 或“)”

题目描述:
# The goal of this exercise is to convert a string to a new string where each character
# in the new string is "(" if that character appears only once in the original string,
# or ")" if that character appears more than once in the original string.
# Ignore capitalization when determining if a character is a duplicate

我的解答:
def duplicate_letter(b):
c = {}
for i in b:
if i not in c.keys():
c[i] = 1
elif i in c.keys():
c[i] = c[i] + 1
new_word = b.lower()
for j in new_word:
if c[j] > 1:
new_word = new_word.replace(j, ")")
elif c[j] == 1:
new_word = new_word.replace(j, "(")
return new_word
原文地址:https://www.cnblogs.com/wlj-axia/p/12637780.html