From 04a4c9aaedfac2f3b9145be39f1201362a053a0d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 13:57:41 -0700 Subject: [PATCH] Add solution using .reverse --- data_structures/arrays/strings/palindrome.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 1ebcfbe..03490ac 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -20,12 +20,13 @@ # @return {Boolean} # -# Approach 1: Brute Force +# Approach 1: Using Ruby method .reverse # -# Time Complexity: +# Time Complexity: O(n) # def is_palindrome(s) - s.downcase + letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') + letters_only.reverse == letters_only end s = 'A man, a plan, a canal: Panama'