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: added z_algorithm in strings #1581

Merged
merged 20 commits into from
Sep 4, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updated z_function.cpp
Updated z_function.cpp as per contribution guidelines.
Fixed Link using github markdown syntax
Created a separate function for tests and covered the corner case
  • Loading branch information
RitikaGupta8734 committed Aug 31, 2021
commit ad4dad5ae37f23db5c9d9e84d2f9a56bc2a53a96
16 changes: 10 additions & 6 deletions strings/z_function.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* @file
* @brief The Z function for finding occurences of a pattern
* @brief The [Z function](https://cp-algorithms.com/string/z-function.html) for finding occurences of a pattern
* within a piece of text with time and space complexity O(n + m)
* @details
* DESCRIPTION
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* 1. The Z-function for a string is an array of length n where the
* i-th element is equal to the greatest number of characters starting
* from the position i that coincide with the first characters of s.
* 2. Eg string : ababb then z[2]=2 as s[2]=s[0] and s[3]=s[1] and s[4]!=s[2]
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* LINK
* https://cp-algorithms.com/string/z-function.html
* @author [Ritika Gupta](https://github.com/RitikaGupta8734)
*/

Expand Down Expand Up @@ -56,20 +54,26 @@ std::vector<int> find_pat_in_text(const std::string &pattern, const std::string
return matching_indexes;
}


/** Main function */
int main() {
void testing(){
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
// usual case
std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
std::string pattern1 = "abc";

std::vector<int> matching_indexes1=find_pat_in_text(pattern1,text1);
assert((matching_indexes1 == std::vector<int>{10,14,18,30,33}));

// corner case
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
std::string text2 = "greengrass";
std::string pattern2 = "abc";

std::vector<int> matching_indexes2=find_pat_in_text(pattern2,text2);
assert((matching_indexes2 == std::vector<int>{}));
}

/** Main function */
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
int main() {

testing();
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}