-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_palindrome.cs
76 lines (60 loc) · 2.39 KB
/
Longest_palindrome.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class Longest_palindrome {
[DataTestMethod]
[DataRow("abc0cba1", 7, "'abc0cba1' value test")]
[DataRow("", 0, "empty string test")]
[DataRow(null, 0, "'null' value test")]
[DataRow("a", 1, "'a' value test")]
[DataRow("aa", 2, "'aa' value test")]
[DataRow("baa", 2, "'baa' value test")]
[DataRow("abc0cba1", 7, "'abc0cba1' value test")]
[DataRow("12 21glg", 5, "'12 21glg' value test")]
[DataRow(" ", 3, "empty space test")]
public void Test(string str, int exp, string inf) {
/*
longest_palindrome
Longest Palindrome
Find the length of the longest substring in the given string s that is the same in reverse.
As an example, if the input was “I like racecars that go fast”, the substring (racecar) length would be 7.
If the length of the input string is 0, the return value must be 0.
Example:
"a" -> 1
"aab" -> 2
"abcde" -> 1
"zzbaabcd" -> 4
"" -> 0
*/
Console.WriteLine(inf);
Assert.AreEqual(exp, GetLongestPalindrome(str));
}
// split, reverse, compare
public static int GetLongestPalindrome(string str) {
if (string.IsNullOrEmpty(str)) return 0;
Func<string, string, int> compareStrBeginnings = (string str1, string str2) => {
int lenEqual = 0;
for (int i = 0; i < Math.Min(str1.Length, str2.Length); i++) {
if (str2[i] == str1[i]) lenEqual++;
else break;
}
return lenEqual * 2;
};
string first, second;
int maxPalindrome = 1, lenEqual = 0;
for (int ind = 0; ind < str.Length - 1; ind++) {
first = string.Concat(str.Substring(0, ind + 1).Reverse());
second = str.Substring(ind + 1);
lenEqual = compareStrBeginnings(first, second);
maxPalindrome = maxPalindrome < lenEqual ? lenEqual : maxPalindrome;
if (ind < str.Length - 2) {
first = string.Concat(str.Substring(0, ind + 1).Reverse());
second = str.Substring(ind + 2);
lenEqual = compareStrBeginnings(first, second) + 1;
maxPalindrome = maxPalindrome < lenEqual ? lenEqual : maxPalindrome;
}
}
return maxPalindrome;
}
}