From 7427e794562532db8432f9e8f6ba61e6813eb1cb Mon Sep 17 00:00:00 2001 From: Jane Date: Fri, 21 Sep 2018 13:52:21 -0700 Subject: [PATCH 1/2] Completed Palindrome Check --- lib/palindrome_check.rb | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_check.rb b/lib/palindrome_check.rb index d60efd0..0d00c85 100644 --- a/lib/palindrome_check.rb +++ b/lib/palindrome_check.rb @@ -1,5 +1,42 @@ # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. +def string_reverse(my_string) + i = 0 + j = my_string.length - 1 + + while i < j + placeholder = my_string[i] + my_string[i] = my_string[j] + my_string[j] = placeholder + i += 1 + j -= 1 + end + return my_string +end + def palindrome_check(my_phrase) - raise NotImplementedError + return false if my_phrase.nil? + + my_phrase_original= [] + my_phrase_reversed = [] + + i = 0 + while i < my_phrase.length + if my_phrase[i] != " " + my_phrase_original << my_phrase[i] + my_phrase_reversed << my_phrase[i] + end + i += 1 + end + + string_reverse(my_phrase_reversed) + + i = 0 + while i < my_phrase_original.length + return false if my_phrase_original[i] != my_phrase_reversed[i] + i += 1 + end + + return true + end From 8d52364c2fff624122d93696f74207ea0522dcae Mon Sep 17 00:00:00 2001 From: Jane Date: Mon, 29 Oct 2018 14:07:00 -0700 Subject: [PATCH 2/2] added time and space complexity --- lib/palindrome_check.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_check.rb b/lib/palindrome_check.rb index 0d00c85..6e73879 100644 --- a/lib/palindrome_check.rb +++ b/lib/palindrome_check.rb @@ -1,5 +1,8 @@ # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. +# Time Complexity: O(n) +# Space Complexity: O(n) + def string_reverse(my_string) i = 0 j = my_string.length - 1 @@ -16,7 +19,7 @@ def string_reverse(my_string) def palindrome_check(my_phrase) return false if my_phrase.nil? - + my_phrase_original= [] my_phrase_reversed = []