Replace Duplicate String Characters
<p>I need to convert a string <code>word</code> where each character that appears only once should be appear as <code>'('</code> in the new string. Any duplicate characters in the original string should be replaced with <code>')'</code>. </p>

<p>My code below...</p>

<pre><code>def duplicate_encode(word):
new_word = ''
for char in word:
    if len(char) &gt; 1:
        new_word += ')'
    else:
        new_word += '('
return new_word
</code></pre>

<p>The test I'm not passing is as follows: </p>

<p>'((((((' should equal '()()()'</p>

<p>This would suggest that, if for example, the input is "recede," the output should read <code>()()()</code>. </p>

<p>Seems like your result is based on the number of occurrences of a character in the word, you can use <code>Counter</code> to keep track of that:</p>

<pre><code>def duplicate_encode(word):
    from collections import Counter

    word = word.lower()              # to disregard case
    counter = Counter(word)
    new_word = ''
    for char in word:
        if counter[char] &gt; 1:        # if the character appears more than once in the word 
                                     # translate it to )
            new_word += ')'
        else:
            new_word += '('
    return new_word

duplicate_encode('recede')
# '()()()'
</code></pre>

python string replace duplicates iteration