File tree 4 files changed +78
-6
lines changed
src/interview-classics/is-palindrome
4 files changed +78
-6
lines changed Original file line number Diff line number Diff line change 2
2
const charsToRemove = / [ . , : ; ? ! \s ] / g
3
3
4
4
/**
5
- * Checks if a string is a palindrome.
5
+ * Checks if a string is a palindrome. Uses srtring and array methods.
6
6
*
7
7
* Time complexity: O(n)
8
8
* Space complexity: O(n)
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change 1
1
const algorithms = [
2
- require ( './is-palindrome-method' )
2
+ require ( './is-palindrome-method' ) ,
3
+ require ( './is-palindrome-while' )
3
4
]
4
5
5
6
module . exports = algorithms
Original file line number Diff line number Diff line change @@ -8,33 +8,60 @@ const palindrome = `Anita! LAva :
8
8
9
9
algorithms . forEach ( ( { fun, id } ) => {
10
10
describe ( `Palindrome test algorithm "${ id } "` , ( ) => {
11
- it ( 'identifies strings that are palindromes ' , ( ) => {
11
+ it ( 'identifies palinfromes of length 0 ' , ( ) => {
12
12
assert . equal (
13
- fun ( palindrome ) ,
13
+ fun ( '' ) ,
14
14
true
15
15
)
16
+ } )
16
17
18
+ it ( 'identifies palindromes of length 1' , ( ) => {
17
19
assert . equal (
18
- fun ( '' ) ,
20
+ fun ( 'x ' ) ,
19
21
true
20
22
)
23
+ } )
21
24
25
+ it ( 'identifies small palindromes of even length' , ( ) => {
22
26
assert . equal (
23
27
fun ( '123321' ) ,
24
28
true
25
29
)
26
30
} )
27
31
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' , ( ) => {
29
47
assert . equal (
30
48
fun ( 'This is not a palindrome' ) ,
31
49
false
32
50
)
51
+ } )
33
52
53
+ it ( 'identifies one-word non-palindromes of even length' , ( ) => {
34
54
assert . equal (
35
55
fun ( '123123' ) ,
36
56
false
37
57
)
38
58
} )
59
+
60
+ it ( 'identifies one-word non-palindromes of odd length' , ( ) => {
61
+ assert . equal (
62
+ fun ( '1230123' ) ,
63
+ false
64
+ )
65
+ } )
39
66
} )
40
67
} )
You can’t perform that action at this time.
0 commit comments