Skip to content

z function added #1362

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

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Changes from all commits
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
33 changes: 33 additions & 0 deletions Strings/ZFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Initialisation of strings algorithm Z function
// detailed review and proper examples can be seen on the link bewlow
//
// Link of algorithm review: https://www.geeksforgeeks.org/z-algorithm-linear-time-pattern-searching-algorithm/
package Strings;
Copy link
Member

Choose a reason for hiding this comment

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

Kindly add a description of your code as this repository is for learning purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okey

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any standards that this repo follows? If so can you show me an example of the description so I meet that standards.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @deadshotsb I have added description and link of the algorithm review

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the inconvenience, @vakhokoto, kindly give a desciption instead of providing links, you can give an example case if needed.


public class ZFunction {
private String string;

public ZFunction(String string){
this.string = string;
}

public int[] getArray(){
int length = string.length();
int[] z = new int[length];
int l = 0, r = 0;
for (int i=0; i<length; i++){
if (i > l && i <= r){
z[i] = Math.min(z[i - l], r - i + 1);
}
while (i + z[i] < length && string.charAt(z[i]) == string.charAt(i + z[i])){
z[i]++;
}
if (i + z[i] > r){
l = i;
r = i + z[i] - 1;
}
}

return z;
}
}