forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
39 lines (36 loc) · 891 Bytes
/
Solution.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
39
// https://leetcode.com/problems/shortest-palindrome/
using System.Text;
public partial class Solution
{
public string ShortestPalindrome(string s)
{
for (var i = s.Length - 1; i >= 0; --i)
{
var k = i;
var j = 0;
while (j < k)
{
if (s[j] == s[k])
{
++j;
--k;
}
else
{
break;
}
}
if (j >= k)
{
var sb = new StringBuilder(s.Length * 2 - i - 1);
for (var l = s.Length - 1; l >= i + 1; --l)
{
sb.Append(s[l]);
}
sb.Append(s);
return sb.ToString();
}
}
return string.Empty;
}
}