Skip to content
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;
}
}