Skip to content

Commit 6f1e35e

Browse files
committed
125
1 parent bd52434 commit 6f1e35e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

ruby/125-Valid-Palindrome.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
 (0)