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>Your Code is Good just need some alteration it will be great.</p>

<pre><code>def duplicate_encode(word):
    """
    To replace the duplicate letter with ")" in a string.
    if given letter is unique it replaced with "("
    """
    word_dict = {}   # initialize a dictionary
    new_word = "" 
    for i in set(word):   # this loop is used to count duplicate words
        word_count = word.count(i)
        word_dict[i] = word_count   # add letter and count of the letter to dictionary
    for i in word:
        if word_dict[i] &gt; 1:
            new_word += ")"
        else:
            new_word += "("
    print new_word

duplicate_encode("recede")
</code></pre>

<p>I think you got the answer :)</p>

python string replace duplicates iteration