Replace ' ? ' in a string such that no two adjacent characters are same | Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given String ; If the first character is '? ; Traverse the String [ 1 , N - 1 ] ; If the current character is '? ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is '? ; Change character ; Check with previous character ; Return the resultant String ; Driver Code ; Given String S ; Function Call
def changeString ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE s = [ ' ▁ ' ] * ( len ( S ) ) NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT s [ i ] = S [ i ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 0 ] == ' ? ' ) : NEW_LINE INDENT s [ 0 ] = ' a ' NEW_LINE if ( s [ 0 ] == s [ 1 ] ) : NEW_LINE INDENT s [ 0 ] = chr ( ord ( s [ 0 ] ) + 1 ) NEW_LINE DEDENT DEDENT for i in range ( 1 , N - 1 ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( s [ i ] == ' ? ' ) : NEW_LINE INDENT s [ i ] = ' a ' NEW_LINE if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( s [ N - 1 ] == ' ? ' ) : NEW_LINE INDENT s [ N - 1 ] = ' a ' NEW_LINE if ( s [ N - 1 ] == s [ N - 2 ] ) : NEW_LINE INDENT s [ N - 1 ] += 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ? a ? a " NEW_LINE print ( changeString ( S ) ) NEW_LINE DEDENT