-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution214.cs
38 lines (32 loc) · 1.24 KB
/
Solution214.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace LeetCode.Solutions;
public class Solution214
{
/// <summary>
/// 214. Shortest Palindrome - Hard
/// <a href="https://leetcode.com/problems/shortest-palindrome">See the problem</a>
/// </summary>
public string ShortestPalindrome(string s)
{
var n = s.Length;
var rev = new string(s.Reverse().ToArray()); // reversed s
var newS = s + "#" + rev; // string containing s and reversed s separated by #
var pi = new int[newS.Length]; // array of prefix function values
for (var i = 1; i < newS.Length; i++)
{
var j = pi[i - 1]; // previous prefix function value
// find the longest prefix of the string ending at i that is also a suffix
while (j > 0 && newS[i] != newS[j])
{
j = pi[j - 1];
}
// if the next character is the same, increment the prefix function value
if (newS[i] == newS[j])
{
j++;
}
// store the prefix function value
pi[i] = j;
}
return rev.Substring(0, n - pi[newS.Length - 1]) + s; // add the missing characters to the beginning of s
}
}