Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

KMP algorithm-LPS array #433

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Binary file added Beginner Level πŸ“/KMP algorithm/KMP.class
Binary file not shown.
Binary file added Beginner Level πŸ“/KMP algorithm/Solution.class
Binary file not shown.
35 changes: 35 additions & 0 deletions Beginner Level πŸ“/KMP algorithm/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution{
public static void main(String args[]){
KMP obj = new KMP();
int a = obj.lps("ABCDABCDAACBDABCD");
System.out.println("The longest prefix suffix is : "+a);
}
}

class KMP{
int lps(String str){
int n = str.length();
int pre = 0;
int suf = 1;
char s[] = str.toCharArray();
int arr[] = new int[n];

while(suf<n){
if(s[pre]==s[suf]){
arr[suf] = pre+1;
pre++;
suf++;
}
else{
if(pre == 0){
arr[suf] = 0;
suf++;
}
else{
pre = arr[pre-1];
}
}
}
return arr[n-1];
}
}
3 changes: 3 additions & 0 deletions Beginner Level πŸ“/KMP algorithm/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//Question
To find the longest prefix which is also the longest suffix for a given string as:
"ABCDABCDAACBDABCD" using KMP algorithm.
5 changes: 5 additions & 0 deletions Beginner Level πŸ“/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
},

// Add your details below as above mention

{
"name": "Samriddhi gupta",
"githubUsername": "sg6724"
},

{
"name": "Sharan Swaroop",
Expand Down