code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

02 Palindrome

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

function palindrome(string, start = 0, end = null) {
  if (end === null) {
    end = string.length - 1
  }
  if (string.length < 2) {
    return true
  }
  if (start >= end) {
    return true
  }
  if (string[start] !== string[end]) {
    return false
  } else {
    return palindrome(string, start + 1, end - 1)
  }
}

test('palindrome', () => {
  expect(palindrome('')).toEqual(true)
  expect(palindrome('Ashish')).toEqual(false)
  expect(palindrome('a')).toEqual(true)
  expect(palindrome('tt')).toEqual(true)
  expect(palindrome('tot')).toEqual(true)
  expect(palindrome('tacocat')).toEqual(true)
  expect(palindrome('boring')).toEqual(false)
})

Created 2019-12-02T02:30:51+05:18 · Edit