Skip to content

Commit dd4c3c2

Browse files
committed
Longest Palindrome
1 parent 666c0aa commit dd4c3c2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: src/leetcode2018/LongestPalindrome.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode2018;
2+
3+
import java.util.HashMap;
4+
5+
public class LongestPalindrome {
6+
public int longestPalindrome(String s) {
7+
HashMap<Character, Integer> map = new HashMap<>();
8+
for(char c: s.toCharArray()){
9+
if(map.containsKey(c)){
10+
map.put(c,map.get(c)+1);
11+
}else{
12+
map.put(c,1);
13+
}
14+
}
15+
int cont=0;
16+
for(char c: map.keySet()){
17+
if(map.get(c)>1){
18+
cont= cont+ map.get(c);
19+
}
20+
}
21+
if(cont<s.toCharArray().length)
22+
cont= cont+1;
23+
24+
return cont;
25+
}
26+
}

0 commit comments

Comments
 (0)