Shortest string possible after removal of all pairs of similar adjacent characters | Function to delete pair of adjacent characters which are equal ; Base Case ; Stores the update string ; Traverse the string s ; If two unequal pair of adjacent characters are found ; If two equal pair of adjacent characters are found ; Append the remaining string after removing the pair ; Return the final String ; Function to find the shortest string after repeatedly removing pairs of adjacent characters which are equal ; Stores the resultant String ; Keeps track of previously iterated string ; Update the result after deleting adjacent pair of characters which are similar ; Termination Conditions ; Update pre variable with the value of result ; Case for " Empty ▁ String " ; Driver Code
function removeAdjacent ( s ) { if ( s . length == 1 ) return s ; let sb = " " ; for ( let i = 0 ; i < s . length - 1 ; i ++ ) { let c = s [ i ] ; let d = s [ i + 1 ] ; if ( c != d ) { sb = sb + c ; if ( i == s . length - 2 ) { sb = sb + d ; } } else { for ( let j = i + 2 ; j < s . length ; j ++ ) sb = sb + s [ j ] ; return sb ; } } return sb ; } function reduceString ( s ) { let result = " " ; let pre = s ; while ( true ) { result = removeAdjacent ( pre ) ; if ( result == pre ) break ; pre = result ; } if ( result . length != 0 ) document . write ( result ) ; else document . write ( " " ) ; } let S = " " ; reduceString ( S ) ;