Skip to content

Commit 96cebc2

Browse files
committed
Time: 18 ms (14.52%), Space: 42.3 MB (50.62%) - LeetHub
1 parent 9035ee4 commit 96cebc2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int longestPalindrome(String s) {
3+
HashSet<Character> hashSet = new HashSet<Character>();
4+
int longestPalindromeLength = 0;
5+
6+
for (int i = 0; i < s.length(); i++) {
7+
if (hashSet.contains(s.charAt(i))) {
8+
longestPalindromeLength += 2;
9+
hashSet.remove(s.charAt(i));
10+
} else {
11+
hashSet.add(s.charAt(i));
12+
}
13+
}
14+
15+
if (hashSet.size() > 0) {
16+
longestPalindromeLength++;
17+
}
18+
19+
return longestPalindromeLength;
20+
}
21+
}

0 commit comments

Comments
 (0)