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

Added an O(1) memory solution for the string rotation challenge #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dmonroe85
Copy link

Added a solution that takes the same approach as the original method (s2 in s1 + s1), but it compares the strings in place and requires constant additional memory.

" matching_count, i2 = 0, 0\n",
"\n",
" # Increment i1 and wrap around if necessary\n",
" i1 += 1\n",

Choose a reason for hiding this comment

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

i1 += 1
if i1 == length:
    i1 = 0

can be replaced by:

i1 = (i1 + 1) % length

which is a common idiom for wrap-around semantics in iteration.

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

Successfully merging this pull request may close these issues.

None yet

3 participants