code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

04 Condense Numbers

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function randomSequence(n) {
  let sequence = []
  for (let index = 0; index < n; index++) {
    const num = Math.floor(Math.random() * 10)
    sequence.push(num)
  }
  return sequence.join('')
}

function condenseNumbers(value) {
  let sequence = value.split('')
  let stack = [sequence.pop()]
  while(sequence.length > 0){
    const left = sequence.pop()
    const right = stack.pop()
    if(left !== right){
      stack.push(right)
      stack.push(left)
    }
  }

  // put everthing back to first stack to undo reversed order
  while(stack.length > 0){
    sequence.push(stack.pop())
  }
  return sequence.join('')
}

test('condense Numbers', () => {
  expect(condenseNumbers('54322346')).toEqual('56')
})

Created 2019-11-30T17:46:30+05:30 · Edit