from collections import Counter
def duplicate_encode(word):
cnt = dict(Counter(word.lower()))
str = ''
for i in word:
if i in cnt:
if cnt[i] < 2:
str += '('
else:
str += ')'
return str
本练习的目标是将字符串转换为新字符串,其中如果该字符在原始字符串中仅出现一次,则新字符串中的每个字符为“(”,如果该字符在原始字符串中多次出现,则为“)”细绳。在确定字符是否重复时忽略大小写。
例子 "din" => "((("
"后退" => "()()()"
"成功" => ")())())"
"((@" => "))(("
出于某种原因,这里for i in word.lower():我不清楚为什么有必要添加.lower()