My code:
public class Solution {
public int longestPalindrome(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int counter = 0;
HashSet<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
if (set.contains(curr)) {
counter++;
set.remove(curr);
}
else {
set.add(curr);
}
}
if (!set.isEmpty()) {
return 2 * counter + 1;
}
else {
return 2 * counter;
}
}
}
reference:
https://discuss.leetcode.com/topic/61300/simple-hashset-solution-java
这道题目一开始没理解题意。看了答案,才知道什么意思。
刷题有些浮躁,不思考,就直接看答案。
Anyway, Good luck, Richardo! -- 10/12/2016