From b09c557e20ff7638baf503fc5c5df9254aad0c51 Mon Sep 17 00:00:00 2001 From: Hayden Williams Date: Sun, 28 Oct 2018 21:41:16 -0700 Subject: [PATCH] implement palindrome check method --- lib/palindrome_check.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_check.rb b/lib/palindrome_check.rb index d60efd0..906794a 100644 --- a/lib/palindrome_check.rb +++ b/lib/palindrome_check.rb @@ -1,5 +1,28 @@ # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. def palindrome_check(my_phrase) - raise NotImplementedError + if my_phrase == nil + return false + end + i = 0 + j = my_phrase.length - 1 + while i < j + if my_phrase[i] == " " || my_phrase[j] == " " + until my_phrase[i] != " " + i += 1 + end + until my_phrase[j] != " " + j -= 1 + end + elsif my_phrase[i] != my_phrase[j] + return false + end + i += 1 + j -= 1 + end + return true end + + +# time complexity: O(n) because it must iterate through each element in the array +# space complexity: O(1) because it does not require any additional memory space besides the iteration variables