Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b06b3a

Browse files
authored
merge: Add new IsPalindrome implementation (TheAlgorithms#1046)
1 parent85f428e commit6b06b3a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

‎String/IsPalindrome.js

Lines changed: 36 additions & 0 deletions
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+
exportfunctionisPalindromeIterative(x){
19+
if(typeofx!=='string'&&typeofx!=='number'){
20+
thrownewTypeError('Input must be a string or a number')
21+
}
22+
23+
// Convert x to string whether it's number or string
24+
conststring=x.toString()
25+
constlength=string.length
26+
27+
if(length===1)returntrue
28+
29+
// Apply two pointers technique to compare first and last elements on each iteration
30+
for(letstart=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])returnfalse
33+
}
34+
// If early return in condition inside for loop is not reached, then input is palindrome
35+
returntrue
36+
}

‎String/test/IsPalindrome.test.js

Lines changed: 33 additions & 0 deletions
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+
constnumberInput=6
10+
conststringInput='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)

[8]ページ先頭

©2009-2025 Movatter.jp