-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem214.cs
More file actions
38 lines (33 loc) · 852 Bytes
/
Copy pathProblem214.cs
File metadata and controls
38 lines (33 loc) · 852 Bytes
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
using System.Linq;
namespace ClassLibrary2
{
public class Problem214
{
public static string ShortestPalindrome(string s)
{
for (int i = s.Length - 1; i >= 0; i--)
{
if (IsPalindrome(s, i))
{
if (i < s.Length - 1)
{
return new string(s.Substring(i + 1).Reverse().ToArray()) + s;
}
return s;
}
}
return s;
}
public static bool IsPalindrome(string s, int endPos)
{
for (int i = 0; i <= endPos / 2; i++)
{
if (s[i] != s[endPos - i])
{
return false;
}
}
return true;
}
}
}