Introduction
The Secret Cipher problem asks us to find the lexicographically smallest encrypted string that can be decoded back into the given original string.
The special character * is used to represent a repeated prefix.
For example:
Original:
ababcababcd
Encrypted:
ab*c*dWhen the decoding process encounters *, the characters before * are appended again. Therefore:
ab*c*dcan be decoded as:
ab → abc → ababc → ababcababcdThe challenge is to find the smallest possible encrypted representation efficiently for a string of length up to 100000.
Problem Understanding
Suppose we have:
s = "abab"The string contains two identical halves:
ab | abTherefore, it can be represented as:
ab*because * repeats the characters before it.
Similarly:
zzzzzzcontains repeated portions and can be compressed.
The goal is not simply to remove every possible repeated substring. We need to find the lexicographically smallest valid encrypted string.
Important Observation
Consider:
ababThe first half is:
abThe second half is:
abSince both are equal:
abab = ab + abwe can replace the second copy with:
ab*This means we need a fast way to determine whether a string has a repeated prefix.
This is where the KMP algorithm becomes useful.
KMP Prefix Function / LPS
KMP stands for Knuth-Morris-Pratt.
One important part of KMP is the LPS array.
LPS means:
Longest Proper Prefix which is also a Suffix.
For:
s = "abab"the LPS array is:
0 0 1 2At the last character:
abab
^^
||
abThe prefix "ab" is also a suffix "ab".
Therefore:
lps[3] = 2Why LPS Helps Here
Suppose the current substring has length len.
If its longest prefix/suffix overlap is large enough, we can determine whether the string is made from repeated sections.
The basic relationship is:
period = len - lps[len - 1]For example:
s = "abab"
len = 4
lps = 2
period = 4 - 2
= 2So the repeating unit is:
"ab"and:
"abab"is:
"ab" + "ab"Therefore it can be compressed.
Java Implementation
class Solution {
public String compress(String s) {
int n = s.length();
// Build KMP LPS array
int[] lps = new int[n];
for (int i = 1; i < n; i++) {
int j = lps[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = lps[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
j++;
}
lps[i] = j;
}
StringBuilder ans = new StringBuilder();
int i = n - 1;
while (i >= 0) {
// Current length
int len = i + 1;
// A repeated-half compression is possible
// only when length is even.
if (len % 2 == 0) {
int border = lps[i];
// Minimum repeating period
int period = len - border;
if (border >= len / 2 &&
len % (2 * period) == 0) {
ans.append('*');
// Move to the first half
i = len / 2 - 1;
continue;
}
}
ans.append(s.charAt(i));
i--;
}
return ans.reverse().toString();
}
}Step 1 – Calculate the LPS Array
The first part of the code is:
int[] lps = new int[n];
for (int i = 1; i < n; i++) {
int j = lps[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = lps[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
j++;
}
lps[i] = j;
}This is the standard KMP prefix-function construction.
For:
ababwe get:
Index: 0 1 2 3
String: a b a b
LPS: 0 0 1 2The last value 2 tells us that "ab" is both a prefix and suffix.
Step 2 – Start From the End
After constructing the LPS array:

Join the conversation! Your thoughts help the community grow.