Check for character which is not same in i + 1 and i - 1
for ( var it = 0 ; it < str . length ; it ++ ) { if ( str [ it ] !== s [ i + 1 ] && str [ it ] !== s [ i - 1 ] ) { s [ i ] = str [ it ] ; break ; } }