Sidhemu Sidhemu - 12 days ago 20
Javascript Question

How to get this output using? Javascript to remove consecutive character is a given string to be same?(Bomber algorithm)

How to get this output using? Javascript to remove consecutive character is a given string to be same?(Bomber algorithm)
example:
input 1: aabcccdee
aabcccdee->bcccdee->bdee->bd
output1: bd

input2: abcdeedcbfgf
abcdeedcbfgf->eeabcddcbfgf->abcddcbfgf->abccbfgf->abbfgf->afgf
output2: afgf

Answer

You could check the character with the character at the index before and after and return a new string. Repeat while changing.

function b(s) {
    var t;

    do {
        t = s;
        s = t.split('').filter(function (a, i, aa) {
            return a !== aa[i - 1] && a !== aa[i + 1];
        }).join('');
    } while (s !== t);
    return s;
}

console.log(b('aabcccdee'));
console.log(b('abcdeedcbfgf'));

Comments