java: add «214. Shortest Palindrome»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
9b3e3e9ee4
commit
52718bec4d
1 changed files with 38 additions and 0 deletions
38
java/shortest-palindrome.java
Normal file
38
java/shortest-palindrome.java
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
class Solution {
|
||||||
|
private String convert(String s) {
|
||||||
|
var reversed = new StringBuilder(s).reverse();
|
||||||
|
|
||||||
|
var combined = new StringBuilder(s);
|
||||||
|
combined.append('#');
|
||||||
|
combined.append(reversed);
|
||||||
|
|
||||||
|
return combined.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] makePrefixTable(String s) {
|
||||||
|
var result = new int[s.length()];
|
||||||
|
|
||||||
|
var length = 0;
|
||||||
|
for (int i = 1; i < s.length(); ++i) {
|
||||||
|
while (length > 0 && s.charAt(i) != s.charAt(length)) {
|
||||||
|
length = result[length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.charAt(i) == s.charAt(length)) {
|
||||||
|
++length;
|
||||||
|
}
|
||||||
|
|
||||||
|
result[i] = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String shortestPalindrome(String s) {
|
||||||
|
var prefix = makePrefixTable(convert(s));
|
||||||
|
var length = prefix[prefix.length - 1];
|
||||||
|
|
||||||
|
var suffix = new StringBuilder(s.substring(length)).reverse();
|
||||||
|
return suffix.append(s).toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue