Skip to content

Commit b42b515

Browse files
committed
LongestPalindrome409
1 parent c6b5070 commit b42b515

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5454

5555

56-
# Total: 494
56+
# Total: 495
5757

5858
| Easy | Medium | Hard | - |
5959
|:-------:|:-------:|:----:|:-:|
60-
| 131 | 272 | 81 | 10 |
60+
| 132 | 272 | 81 | 10 |
6161

6262

6363
| Question | Solution | Difficulty |
@@ -362,6 +362,7 @@
362362
| [405. Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConvertANumberToHexadecimal405.java) | Easy |
363363
| [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/QueueReconstructionByHeight406.java) | Medium |
364364
| [407. Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TrappingRainWaterII407.java) | Medium |
365+
| [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LongestPalindrome409.java) | Easy |
365366
| [410. Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SplitArrayLargestSum410.java) | Hard |
366367
| [414. Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ThirdMaximumNumber414.java) | Easy |
367368
| [415. Add Strings](https://leetcode.com/problems/add-strings/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/AddStrings415.java) | Easy |

src/LongestIncreasingSubsequence300.java

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public int lengthOfLIS(int[] nums) {
3434

3535
/**
3636
* https://leetcode.com/problems/longest-increasing-subsequence/solution/
37+
* https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
3738
*/
3839
public int lengthOfLIS2(int[] nums) {
3940
int[] dp = new int[nums.length];

src/LongestPalindrome409.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Given a string which consists of lowercase or uppercase letters, find the
3+
* length of the longest palindromes that can be built with those letters.
4+
*
5+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
6+
*
7+
* Note:
8+
* Assume the length of given string will not exceed 1,010.
9+
*
10+
* Example:
11+
* Input:
12+
* "abccccdd"
13+
* Output:
14+
* 7
15+
*
16+
* Explanation:
17+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
18+
*/
19+
20+
public class LongestPalindrome409 {
21+
public int longestPalindrome(String s) {
22+
Map<Character, Integer> map = new HashMap<>();
23+
for (char ch: s.toCharArray()) {
24+
map.put(ch, map.getOrDefault(ch, 0) + 1);
25+
}
26+
27+
int res = 0;
28+
boolean hasOdd = false;
29+
for (int i: map.values()) {
30+
if (i % 2 == 0) {
31+
res += i;
32+
} else {
33+
res += i - 1;
34+
hasOdd = true;
35+
}
36+
}
37+
return res + (hasOdd ? 1 : 0);
38+
}
39+
40+
41+
/**
42+
* https://leetcode.com/problems/longest-palindrome/discuss/89604/Simple-HashSet-solution-Java
43+
*/
44+
public int longestPalindrome2(String s) {
45+
if(s==null || s.length()==0) return 0;
46+
HashSet<Character> hs = new HashSet<Character>();
47+
int count = 0;
48+
for(int i=0; i<s.length(); i++){
49+
if(hs.contains(s.charAt(i))){
50+
hs.remove(s.charAt(i));
51+
count++;
52+
}else{
53+
hs.add(s.charAt(i));
54+
}
55+
}
56+
if(!hs.isEmpty()) return count*2+1;
57+
return count*2;
58+
}
59+
60+
61+
/**
62+
* https://leetcode.com/problems/longest-palindrome/discuss/89610/Simple-Java-Solution-in-One-Pass
63+
*/
64+
public int longestPalindrome3(String s) {
65+
boolean[] map = new boolean[128];
66+
int len = 0;
67+
for (char c : s.toCharArray()) {
68+
map[c] = !map[c]; // flip on each occurrence, false when seen n*2 times
69+
if (!map[c]) len+=2;
70+
}
71+
if (len < s.length()) len++; // if more than len, atleast one single is present
72+
return len;
73+
}
74+
75+
}

0 commit comments

Comments
 (0)