Skip to content

Commit 0bc1b91

Browse files
committed
Finds the longest palindrome
1 parent 7f075e3 commit 0bc1b91

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Others/longestPalindrome.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* Created by Dungeoun on 10/2/16.
6+
*/
7+
public class Solution {
8+
9+
public static int longestPalindrome(String s) {
10+
11+
12+
HashMap<Character, Integer> hash1 = new HashMap<>();
13+
14+
int length =0;
15+
16+
17+
for(int i = 0; i<s.length(); i++){
18+
19+
if(hash1.containsKey(s.charAt(i))==false){
20+
21+
hash1.put(s.charAt(i), 1);
22+
23+
}
24+
25+
else{
26+
27+
hash1.put(s.charAt(i), hash1.get(s.charAt(i))+1);
28+
29+
}
30+
}
31+
32+
for(Map.Entry<Character,Integer> entry: hash1.entrySet()){
33+
34+
if(entry.getValue()%2 == 0){
35+
36+
length = length + entry.getValue();
37+
38+
}
39+
else if(entry.getValue()%2 != 0){
40+
41+
length = length + entry.getValue()-1;
42+
43+
}
44+
45+
46+
}
47+
48+
if(s.length()>length) {
49+
50+
return length + 1;
51+
}
52+
else return length;
53+
54+
}
55+
56+
57+
public static void main(String []args){
58+
59+
int l = longestPalindrome("bb");
60+
61+
System.out.println(l);
62+
63+
}
64+
}

0 commit comments

Comments
 (0)