Skip to content

Commit

Permalink
125
Browse files Browse the repository at this point in the history
  • Loading branch information
TedTran2019 committed Jul 8, 2022
1 parent bd52434 commit 6f1e35e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ruby/125-Valid-Palindrome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def is_palindrome(s)
str = s.downcase.chars.select { |char| /[a-zA-Z0-9]/.match?(char) }.join
idx_start = 0
idx_end = str.length - 1
while idx_start < idx_end
return false if str[idx_start] != str[idx_end]

idx_start += 1
idx_end -= 1
end
true
end

def is_palindrome(s)
idx_start = 0
idx_end = s.length - 1
while idx_start < idx_end
if !/[a-zA-Z0-9]/.match?(s[idx_start])
idx_start += 1
elsif !/[a-zA-Z0-9]/.match?(s[idx_end])
idx_end -= 1
else
return false if s[idx_start].downcase != s[idx_end].downcase

idx_start += 1
idx_end -= 1
end
end
true
end

def is_palindrome(s)
str = s.downcase.chars.select { |char| /[a-zA-Z0-9]/.match?(char) }
str == str.reverse
end

0 comments on commit 6f1e35e

Please sign in to comment.