Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lj palindrome #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
38 changes: 37 additions & 1 deletion lib/palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any comments on the time and space complexity. Can you think through the time and space complexity of your algorithm?
Gentle reminder: Always explain what n stands for while explaining your time and space complexity. That along with your explanation for reasoning behind your answer, is the complete answer for the time and space complexity. In this case, n is the number of characters in the input string parameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback Shruti this is very helpful -I pushed up a refactor on this and included my thoughts on T and S complexity following your feedback above. I'm not confident in in my answer on T complexity when while loops are nested within each other so if you have clarification on calculating those I appreciate it!

raise NotImplementedError
# check for nil
unless my_phrase.nil?
i = 0
last = my_phrase.length - 1
while i < last # O(n/2)
# iterate from head of string until non whitespace char
until my_phrase[i] != ' ' # O(n/2)
i += 1
end

# iterate from tail of string until non whitespace char
until my_phrase[last] != ' ' # O(n/2)
last -= 1
end


# once two non-whitespace characters are found compare them
if my_phrase[i] != my_phrase[last]
return false
else
i += 1
last -= 1
end
end

return true
end

return false
end

# For time complexity I think the overall number of operations is dependent on the
# length of the string/phrase (n) At most 1/2 of the length of the string
# operations will be necessary to iterate over (the head and tail
# increment/decrement in unison) O(n)
# Using this method no additional space is required beyond the
# head and tail incrementors as all non-whitespace characters are checked in
# place and only a boolean is returned O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done! The time complexity is correct. The space complexity of your algorithm is O(1). See my implementation of the same algorithm on https://github.com/Ada-C10/palindrome_check/blob/solution/lib/palindrome_check.rb