Skip to content

Commit e45e928

Browse files
refactor 409
1 parent a4f05f8 commit e45e928

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ Your ideas/fixes/algorithms are more than welcome!
373373
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_412.java)| O(n)|O(1) | |Easy|
374374
|411|[Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_411.java)| O(?)|O(?) | |Hard| NP-Hard, Backtracking, Trie, Recursion
375375
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_410.java)| O(nlogn)|O(1) | |Hard| Binary Search, DP
376+
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_409.java)| O(n)|O(1) | |Easy|
376377
|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_408.java)| O(n)|O(1) | |Easy|
377378
|407|[Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_407.java)| | | |Hard| Heap
378379
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_406.java)| O(nlogn)|O(1) | |Medium| LinkedList, PriorityQueue
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 409. Longest Palindrome
5+
*
6+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
7+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
8+
*
9+
* Note:
10+
* Assume the length of given string will not exceed 1,010.
11+
*
12+
* Example:
13+
* Input:
14+
* "abccccdd"
15+
*
16+
* Output:
17+
* 7
18+
*
19+
* Explanation:
20+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
21+
*/
22+
public class _409 {
23+
public static class Solution1 {
24+
public int longestPalindrome(String s) {
25+
int[] counts = new int[56];
26+
for (char c : s.toCharArray()) {
27+
if (Character.isUpperCase(c)) {
28+
counts[c - 'A' + 33]++;
29+
} else {
30+
counts[c - 'a']++;
31+
}
32+
}
33+
boolean hasOdd = false;
34+
int len = 0;
35+
for (int i = 0; i < 56; i++) {
36+
if (counts[i] % 2 != 0) {
37+
hasOdd = true;
38+
if (counts[i] > 1) {
39+
len += counts[i] - 1;
40+
}
41+
} else {
42+
len += counts[i];
43+
}
44+
}
45+
return hasOdd ? len + 1 : len;
46+
}
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._409;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
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+
}
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy