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

feat: let KMP algorithm return index #2713

Merged
merged 8 commits into from
Sep 1, 2024

Conversation

Yancey2023
Copy link
Contributor

@Yancey2023 Yancey2023 commented Aug 27, 2024

Description of Change

For strings/knuth_morris_pratt.cpp:

fix: if pattern is empty, it should immediately return, otherwise pattern[k] will access out of bounds.

feat: let the KMP algorithm return index and add more tests.

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

@realstealthninja
Copy link
Collaborator

If you have the time could you write tests for this algorithm?

@realstealthninja realstealthninja linked an issue Aug 27, 2024 that may be closed by this pull request
@Yancey2023 Yancey2023 changed the title fix: KMP algorithm feat: let KMP algorithm return index Aug 27, 2024
@Yancey2023
Copy link
Contributor Author

Hi, I've added some test cases! Additionally, I've changed the return value to the starting index of the pattern. Perhaps the usage of std::string::npos and size_t in the code might confuse beginners, but this is based on my reference to std::string::find(), which indeed returns a size_t type and returns std::string::npos when the substring is not found.

Copy link
Collaborator

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

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

Thank you so much! Great work, Feel free to add yourself as one of the authors in the file header comment

strings/knuth_morris_pratt.cpp Outdated Show resolved Hide resolved
@realstealthninja realstealthninja added Improvement improvement in previously written codes 1st Review: Passed labels Aug 29, 2024
strings/knuth_morris_pratt.cpp Outdated Show resolved Hide resolved
@Yancey2023
Copy link
Contributor Author

Yancey2023 commented Aug 31, 2024

I don't understand:

#ifdef _MSC_VER
#include <string>  // use this for MS Visual C++
#else
#include <cstring>
#endif

@Yancey2023
Copy link
Contributor Author

Yancey2023 commented Aug 31, 2024

I don't understand:

#ifdef _MSC_VER
#include <string>  // use this for MS Visual C++
#else
#include <cstring>
#endif

Why not just use #include <string>?

@realstealthninja
Copy link
Collaborator

It's old code feel free to remove

@Yancey2023
Copy link
Contributor Author

Thanks. I have deleted it.

Copy link
Collaborator

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

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

Thank you for your contributions! Happy to have this algorithm refactored! ❤️

@realstealthninja realstealthninja merged commit 502a953 into TheAlgorithms:master Sep 1, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1st Review: Passed Improvement improvement in previously written codes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] KMP algorithm
2 participants