Skip to content

Commit 91bfcec

Browse files
committed
feat: implement while-loop solution
1 parent a187e1d commit 91bfcec

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

src/interview-classics/is-palindrome/is-palindrome-method.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const charsToRemove = /[.,:;?!\s]/g
33

44
/**
5-
* Checks if a string is a palindrome.
5+
* Checks if a string is a palindrome. Uses srtring and array methods.
66
*
77
* Time complexity: O(n)
88
* Space complexity: O(n)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Characters to remove from string in order to normalize it
2+
const charsToIgnore = /[.,:;?!\s]/
3+
4+
/**
5+
* Checks if a string is a palindrome. Uses a while loop.
6+
*
7+
* Time complexity: O(n)
8+
* Space complexity: O(1)
9+
*
10+
* @param {string} str String to be checked.
11+
* @returns {boolean} Whether the string is a palindrome.
12+
*/
13+
const isPalindrome = (str) => {
14+
let leftIdx = 0
15+
let rightIdx = str.length - 1
16+
17+
while (leftIdx < rightIdx) {
18+
// Skip left characters that are in the ignore regex
19+
while (charsToIgnore.test(str[leftIdx])) {
20+
leftIdx++
21+
}
22+
23+
// Skip right characters that are in the ignore regex
24+
while (charsToIgnore.test(str[rightIdx])) {
25+
rightIdx--
26+
}
27+
28+
// Different characters, not a palindrome
29+
if (str[leftIdx].toLowerCase() !== str[rightIdx].toLowerCase()) {
30+
return false
31+
}
32+
33+
// Equal characters, continue to next characters
34+
leftIdx++
35+
rightIdx--
36+
}
37+
38+
return true
39+
}
40+
41+
module.exports = {
42+
fun: isPalindrome,
43+
id: 'while-loop'
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const algorithms = [
2-
require('./is-palindrome-method')
2+
require('./is-palindrome-method'),
3+
require('./is-palindrome-while')
34
]
45

56
module.exports = algorithms

src/interview-classics/is-palindrome/is-palindrome.test.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,60 @@ const palindrome = `Anita! LAva :
88

99
algorithms.forEach(({ fun, id }) => {
1010
describe(`Palindrome test algorithm "${id}"`, () => {
11-
it('identifies strings that are palindromes', () => {
11+
it('identifies palinfromes of length 0', () => {
1212
assert.equal(
13-
fun(palindrome),
13+
fun(''),
1414
true
1515
)
16+
})
1617

18+
it('identifies palindromes of length 1', () => {
1719
assert.equal(
18-
fun(''),
20+
fun('x'),
1921
true
2022
)
23+
})
2124

25+
it('identifies small palindromes of even length', () => {
2226
assert.equal(
2327
fun('123321'),
2428
true
2529
)
2630
})
2731

28-
it('identifies strings that are not palindromes', () => {
32+
it('identifies small palindromes of odd length', () => {
33+
assert.equal(
34+
fun('1230321'),
35+
true
36+
)
37+
})
38+
39+
it('identifies palindromes with multiple whitespace', () => {
40+
assert.equal(
41+
fun(palindrome),
42+
true
43+
)
44+
})
45+
46+
it('identifies sentence-like non-palindromes', () => {
2947
assert.equal(
3048
fun('This is not a palindrome'),
3149
false
3250
)
51+
})
3352

53+
it('identifies one-word non-palindromes of even length', () => {
3454
assert.equal(
3555
fun('123123'),
3656
false
3757
)
3858
})
59+
60+
it('identifies one-word non-palindromes of odd length', () => {
61+
assert.equal(
62+
fun('1230123'),
63+
false
64+
)
65+
})
3966
})
4067
})

0 commit comments

Comments
 (0)