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