File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ def is_palindrome ( s )
2
+ str = s . downcase . chars . select { |char | /[a-zA-Z0-9]/ . match? ( char ) } . join
3
+ idx_start = 0
4
+ idx_end = str . length - 1
5
+ while idx_start < idx_end
6
+ return false if str [ idx_start ] != str [ idx_end ]
7
+
8
+ idx_start += 1
9
+ idx_end -= 1
10
+ end
11
+ true
12
+ end
13
+
14
+ def is_palindrome ( s )
15
+ idx_start = 0
16
+ idx_end = s . length - 1
17
+ while idx_start < idx_end
18
+ if !/[a-zA-Z0-9]/ . match? ( s [ idx_start ] )
19
+ idx_start += 1
20
+ elsif !/[a-zA-Z0-9]/ . match? ( s [ idx_end ] )
21
+ idx_end -= 1
22
+ else
23
+ return false if s [ idx_start ] . downcase != s [ idx_end ] . downcase
24
+
25
+ idx_start += 1
26
+ idx_end -= 1
27
+ end
28
+ end
29
+ true
30
+ end
31
+
32
+ def is_palindrome ( s )
33
+ str = s . downcase . chars . select { |char | /[a-zA-Z0-9]/ . match? ( char ) }
34
+ str == str . reverse
35
+ end
You can’t perform that action at this time.
0 commit comments