Skip to content

Commit 6b06b3a

Browse files
authored
merge: Add new IsPalindrome implementation (#1046)
1 parent 85f428e commit 6b06b3a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

String/IsPalindrome.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @function isPalindromeIterative
3+
* @description isPalindromeIterative function checks whether the provided input is palindrome or not
4+
* @param {String | Number} x - The input to check
5+
* @return {boolean} - Input is palindrome or not
6+
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
7+
*/
8+
9+
/*
10+
* Big-O Analysis
11+
* Time Complexity
12+
- O(N) on average and worst case scenario as input is traversed in linear fashion
13+
- O(N) on best case scenario, even when input has length of 1, because toString() method takes O(N)
14+
* Space Complexity
15+
- O(1)
16+
*/
17+
18+
export function isPalindromeIterative (x) {
19+
if (typeof x !== 'string' && typeof x !== 'number') {
20+
throw new TypeError('Input must be a string or a number')
21+
}
22+
23+
// Convert x to string whether it's number or string
24+
const string = x.toString()
25+
const length = string.length
26+
27+
if (length === 1) return true
28+
29+
// Apply two pointers technique to compare first and last elements on each iteration
30+
for (let start = 0, end = length - 1; start < end; start++, end--) {
31+
// Early return if compared items are different, input is not a palindrome
32+
if (string[start] !== string[end]) return false
33+
}
34+
// If early return in condition inside for loop is not reached, then input is palindrome
35+
return true
36+
}

String/test/IsPalindrome.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { isPalindromeIterative } from '../IsPalindrome'
2+
3+
describe('isPalindrome', () => {
4+
it('expects to return true with empty string', () => {
5+
expect(isPalindromeIterative('')).toEqual(true)
6+
})
7+
8+
it('expects to return true when length of input is 1', () => {
9+
const numberInput = 6
10+
const stringInput = 'a'
11+
expect(isPalindromeIterative(numberInput)).toEqual(true)
12+
expect(isPalindromeIterative(stringInput)).toEqual(true)
13+
})
14+
15+
it('expects to return true when input is palindrome', () => {
16+
expect(isPalindromeIterative(121)).toEqual(true)
17+
expect(isPalindromeIterative('yooy')).toEqual(true)
18+
expect(isPalindromeIterative('19noon91')).toEqual(true)
19+
expect(isPalindromeIterative('!*tyyt*!')).toEqual(true)
20+
})
21+
22+
it('expects to return false when input is not palindrome', () => {
23+
expect(isPalindromeIterative('hello')).toEqual(false)
24+
expect(isPalindromeIterative(189)).toEqual(false)
25+
expect(isPalindromeIterative('!*98[!')).toEqual(false)
26+
})
27+
28+
it('expects to throw error when input is not a string or a number', () => {
29+
expect(() => isPalindromeIterative(undefined)).toThrowError()
30+
expect(() => isPalindromeIterative({ key: 'val' })).toThrowError()
31+
expect(() => isPalindromeIterative([])).toThrowError()
32+
})
33+
})

0 commit comments

Comments
 (0)