VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-prefix-also-suffix/

⇱ Longest prefix which is also suffix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest prefix which is also suffix

Last Updated : 25 Jul, 2025

Given a string s, find the length of the longest proper prefix which is also a suffix. A proper prefix is a prefix that doesn’t include whole string. For example, prefixes of "abc" are "", "a", "ab" and "abc" but proper prefixes are "", "a" and "ab" only.

Examples: 

Input: s = "aabcdaabc"
Output: 4
Explanation: The string "aabc" is the longest proper prefix which is also the suffix.

Input: s = "ababab"
Output: 4
Explanation: The string "abab" is the longest proper prefix which is also the suffix.

Input: s = "aaaa"
Output: 3
Explanation: The string "aaa" is the longest proper prefix which is also the suffix.

[Naive approach] Naive Prefix-Suffix Match - O(n2) Time and O(1) Space

The idea is to compare each proper prefix of the string with its corresponding suffix. A proper prefix has a length ranging from 0 to n - 1. For each possible length, we check if the prefix of that size matches the suffix of the same size. If a match is found, we update the result with the maximum matching length. This brute-force method ensures we find the longest prefix which is also a suffix.


Output
4

[Expected approach] Using LPS of KMP Algorithm

The idea is to use the preprocessing step of the KMP (Knuth-Morris-Pratt) algorithm. In this step, we construct an LPS (Longest Prefix Suffix) array, where each index i stores the length of the longest proper prefix of the substring str[0...i] that is also a suffix of the same substring. The value at the last index of the LPS array represents the length of the longest proper prefix which is also a suffix for the entire string.


Output
4

Time Complexity: O(n) we iterate through the string once using the i pointer, and in the worst case, each character is processed at most twice (once when matched, once when falling back via len = lps[len - 1]).
Auxiliary Space: O(n)

[Efficient Approach] Double Hash Prefix-Suffix Check - O(n) Time and O(1) Space

The idea is to use double rolling hash to compute and compare the hash values of the prefix and suffix of every possible length from 1 to n-1.
We update prefix and suffix hashes in each iteration and check if they match. If both hashes match, we update the result with the current length. Using two moduli reduces the risk of hash collisions and ensures correctness.

Step by Step Implementation:

  • Initialize constants - two bases (base1, base2) and two mod values (mod1, mod2) for double hashing.
  • Initialize:
    -> Prefix powers p1, p2 as 1.
    -> Hash arrays: hash1 and hash2 (both of size 2) to store prefix and suffix hashes.
    -> ans to store the final result.
  • Loop through the string from index 0 to n-2:
    -> Update hash1[0] and hash1[1] by adding current character's weighted value to the prefix hash.
    -> Update hash2[0] and hash2[1] by appending character from the end to the suffix hash.
  • If both pairs match, update ans = i + 1.
  • Multiply p1 with base1, and p2 with base2 under mod.
  • Return ans as the length of the longest prefix which is also a suffix.
Comment