22. Longest Prefix Suffix
The problem can be found at the following link: Question Link
Problem Description
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
Note: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Examples:
Input: str = "abab"
Output: 2
Explanation: "ab" is the longest proper prefix and suffix.
Input: str = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper prefix and suffix.
My Approach
LPS Array Construction:
Use an array
lpsArr
wherelpsArr[i]
stores the length of the longest proper prefix which is also a proper suffix for the substringstr[0...i]
.
Initialization:
Start with the first character; the longest prefix suffix for a single character is always 0.
Iterate through the String:
For each character in the string, update the
lpsArr
:If the current character matches the character at index
j
, incrementj
and setlpsArr[i]
toj
.If thereβs a mismatch and
j
is not zero, updatej
using the previously calculated values inlpsArr
.
Final Answer:
The value at the last index of
lpsArr
will give the length of the longest proper prefix which is also a proper suffix.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(|str|), where |str| is the length of the string, as we only iterate through the string once.
Expected Auxiliary Space Complexity: O(|str|), due to the LPS array that stores prefix-suffix lengths for each substring.
Code (C++)
class Solution {
public:
int lps(const string& str) {
int n = str.size();
if (n == 0) return 0;
vector<int> lpsArr(n, 0);
int j = 0;
for (int i = 1; i < n; i++) {
while (j > 0 && str[i] != str[j]) {
j = lpsArr[j - 1];
}
if (str[i] == str[j]) {
j++;
}
lpsArr[i] = j;
}
return lpsArr[n - 1];
}
};
Code (Java)
class Solution {
public int lps(String str) {
int n = str.length();
if (n == 0) return 0;
int[] lpsArr = new int[n];
int j = 0;
for (int i = 1; i < n; i++) {
while (j > 0 && str.charAt(i) != str.charAt(j)) {
j = lpsArr[j - 1];
}
if (str.charAt(i) == str.charAt(j)) {
j++;
}
lpsArr[i] = j;
}
return lpsArr[n - 1];
}
}
Code (Python)
class Solution:
def lps(self, s: str) -> int:
n = len(s)
if n == 0:
return 0
lpsArr = [0] * n
j = 0
for i in range(1, n):
while j > 0 and s[i] != s[j]:
j = lpsArr[j - 1]
if s[i] == s[j]:
j += 1
lpsArr[i] = j
return lpsArr[n - 1]
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit my LinkedIn: Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
β Star this repository if you find it helpful or intriguing! β
πVisitor Count
Last updated