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

lj palindrome #37

wants to merge 3 commits into from

Conversation

elle-johnnie
Copy link

No description provided.

flag = true
while i < last
if phrase[i] != phrase[last]
flag = false

Choose a reason for hiding this comment

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

Instead of setting the flag as false, you could consider stopping the loop and returning false here. If the first check itself does not match, you don't need to check the rest to conclude that it is not a palindrome.

Copy link
Author

Choose a reason for hiding this comment

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

Ah right, good catch!

i = 0
last = phrase.length - 1
flag = true
while i < last

Choose a reason for hiding this comment

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

Can you think of a way to account for skipping consecutive white spaces in this loop? (Instead of altering the input string parameter by removing spaces in it.)

Copy link
Author

Choose a reason for hiding this comment

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

Without altering the input parameter, I suppose I could check for spaces in the comparison method. So if phrase[i] == ' ' then increment i before comparing or if phrase[last] == ' ' then decrement last before comparing. In this case I'm guessing this would reduce my space complexity and potentially my time complexity. So no additional string would have to be stored and while iterating across the array in for comparison of i (start) and last (end) characters the space ( ' ') occurrence could be accounted for as they came up instead of necessitating that the entire string is iterated across first. For example in my implementation a string with no spaces will get iterated across to check for spaces and a new string gets stored even, if its an exact copy of the original. Taking your modified approach would definitely eliminate additional memory use and redundant iteration of the entire string.

Choose a reason for hiding this comment

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

Nice! Good thinking!

@@ -1,5 +1,44 @@
# 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!

@shrutivanw
Copy link

I added some comments inline. After you give those some thought, take a look at my solution here: https://github.com/Ada-C10/palindrome_check/blob/solution/lib/palindrome_check.rb

@shrutivanw
Copy link

Nice work! See comments added inline.

# 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants