Simple Java Solution


  • 0
    H
    public class Solution {
        public int longestPalindrome(String s) {
            Set<Character> set = new HashSet<>();
            int count = 0;
            for (char c : s.toCharArray()) {
                if (set.contains(c)) {
                    set.remove(c);
                    count += 2;
                } else {
                    set.add(c);
                }
            }
            return count + (set.size() > 0 ? 1 : 0);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.