Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d58fe9

Browse files
refactor 409
1 parent41e5bd5 commit6d58fe9

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
packagecom.fishercoder.solutions;
22

3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
36
publicclass_409 {
47
publicstaticclassSolution1 {
58
publicintlongestPalindrome(Strings) {
@@ -26,4 +29,32 @@ public int longestPalindrome(String s) {
2629
returnhasOdd ?len +1 :len;
2730
}
2831
}
32+
33+
publicstaticclassSolution2 {
34+
/**
35+
* My completely original solution on 10/14/2021.
36+
*/
37+
publicintlongestPalindrome(Strings) {
38+
Map<Character,Integer>map =newHashMap<>();
39+
for (charc :s.toCharArray()) {
40+
map.put(c,map.getOrDefault(c,0) +1);
41+
}
42+
intans =0;
43+
booleanhasOdd =false;
44+
for (charkey :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+
returnans;
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
importstaticorg.junit.Assert.assertEquals;
88

99
publicclass_409Test {
10-
privatestatic_409.Solution1solution1;
11-
12-
@BeforeClass
13-
publicstaticvoidsetup() {
14-
solution1 =new_409.Solution1();
15-
}
16-
17-
@Test
18-
publicvoidtest1() {
19-
assertEquals(7,solution1.longestPalindrome("abccccdd"));
20-
}
21-
22-
@Test
23-
publicvoidtest2() {
24-
assertEquals(7,solution1.longestPalindrome("abccAccdd"));
25-
}
26-
27-
@Test
28-
publicvoidtest3() {
29-
assertEquals(983,solution1.longestPalindrome(
30-
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
31-
}
32-
33-
@Test
34-
publicvoidtest4() {
35-
assertEquals(3,solution1.longestPalindrome("ccc"));
36-
}
10+
privatestatic_409.Solution1solution1;
11+
privatestatic_409.Solution2solution2;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_409.Solution1();
16+
solution2 =new_409.Solution2();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
assertEquals(7,solution1.longestPalindrome("abccccdd"));
22+
assertEquals(7,solution2.longestPalindrome("abccccdd"));
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
assertEquals(7,solution1.longestPalindrome("abccAccdd"));
28+
assertEquals(7,solution2.longestPalindrome("abccAccdd"));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(983,solution1.longestPalindrome(
34+
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
35+
assertEquals(983,solution2.longestPalindrome(
36+
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
37+
}
38+
39+
@Test
40+
publicvoidtest4() {
41+
assertEquals(3,solution1.longestPalindrome("ccc"));
42+
assertEquals(3,solution2.longestPalindrome("ccc"));
43+
}
3744
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp