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

BarbaraWidjon, Edges, palindrome_check #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

BarbaraWidjono
Copy link

**Time complexity: O(n/2) --> O(n). The function will start iterations from index of 0 and index of length-1. Therefore, the function will iterate through half of the string (n/2). Constants are dropped, so the final time complexity is O(n)

**Space complexity: O(1) since additional memory space is not necessary for the execution of this algorithm.

end

def find_start_index(my_phrase, index_val)

Choose a reason for hiding this comment

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

As due diligence, consider passing my_phrase.length as the third parameter to this method. Then, in the loop on line 37, you can first confirm that new_index is not out of bounds before checking the character at the index.

Because you're coding in Ruby, the algorithm works without it since at index length, the value will be nil and the loop will terminate. However, if you were coding in any other language, your algorithm will need to be more robust and account for index bounds.

new_index = index_val

if my_phrase[new_index] == " "
until my_phrase[new_index] != " "

Choose a reason for hiding this comment

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

confirm that new_index is greater than or equal to 0 before comparing the value at that index with a white space.

@shrutivanw
Copy link

Nice work on the algorithm. I added a few comments to make your algorithm further robust.

The time and space complexities and your explanation is correct. The only suggestion I have is to always explain what n stands for. In this case n is the length of the input string or the number of characters in the input string.

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