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 ; Given String S ; Function Call
function changeString ( S ) { let s = S . split ( " " ) ; let N = S . length ; if ( s [ 0 ] == ' ' ) { s [ 0 ] = ' ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] = String . fromCharCode ( s [ 0 ] . charCodeAt ( 0 ) + 1 ) ; } } for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ' ) { s [ i ] = ' ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } } } if ( s [ N - 1 ] == ' ' ) { s [ N - 1 ] = ' ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } let ans = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { ans += s [ i ] ; } return ans ; } let S = " " ; document . write ( changeString ( S ) ) ;