Skip to content

Commit 6d58fe9

Browse files
refactor 409
1 parent 41e5bd5 commit 6d58fe9

File tree

2 files changed

+65
-27
lines changed

2 files changed

+65
-27
lines changed

src/main/java/com/fishercoder/solutions/_409.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
public class _409 {
47
public static class Solution1 {
58
public int longestPalindrome(String s) {
@@ -26,4 +29,32 @@ public int longestPalindrome(String s) {
2629
return hasOdd ? len + 1 : len;
2730
}
2831
}
32+
33+
public static class Solution2 {
34+
/**
35+
* My completely original solution on 10/14/2021.
36+
*/
37+
public int longestPalindrome(String s) {
38+
Map<Character, Integer> map = new HashMap<>();
39+
for (char c : s.toCharArray()) {
40+
map.put(c, map.getOrDefault(c, 0) + 1);
41+
}
42+
int ans = 0;
43+
boolean hasOdd = false;
44+
for (char key : map.keySet()) {
45+
if (map.getOrDefault(key, 0) % 2 == 0) {
46+
ans += map.getOrDefault(key, 0);
47+
} else {
48+
hasOdd = true;
49+
if (map.containsKey(key)) {
50+
ans += map.getOrDefault(key, 0) - 1;
51+
}
52+
}
53+
}
54+
if (hasOdd) {
55+
ans++;
56+
}
57+
return ans;
58+
}
59+
}
2960
}

src/test/java/com/fishercoder/_409Test.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,38 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _409Test {
10-
private static _409.Solution1 solution1;
11-
12-
@BeforeClass
13-
public static void setup() {
14-
solution1 = new _409.Solution1();
15-
}
16-
17-
@Test
18-
public void test1() {
19-
assertEquals(7, solution1.longestPalindrome("abccccdd"));
20-
}
21-
22-
@Test
23-
public void test2() {
24-
assertEquals(7, solution1.longestPalindrome("abccAccdd"));
25-
}
26-
27-
@Test
28-
public void test3() {
29-
assertEquals(983, solution1.longestPalindrome(
30-
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
31-
}
32-
33-
@Test
34-
public void test4() {
35-
assertEquals(3, solution1.longestPalindrome("ccc"));
36-
}
10+
private static _409.Solution1 solution1;
11+
private static _409.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _409.Solution1();
16+
solution2 = new _409.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(7, solution1.longestPalindrome("abccccdd"));
22+
assertEquals(7, solution2.longestPalindrome("abccccdd"));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(7, solution1.longestPalindrome("abccAccdd"));
28+
assertEquals(7, solution2.longestPalindrome("abccAccdd"));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(983, solution1.longestPalindrome(
34+
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
35+
assertEquals(983, solution2.longestPalindrome(
36+
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
37+
}
38+
39+
@Test
40+
public void test4() {
41+
assertEquals(3, solution1.longestPalindrome("ccc"));
42+
assertEquals(3, solution2.longestPalindrome("ccc"));
43+
}
3744
}

0 commit comments

Comments
 (0)