Stupid easy to understand Java solution.


  • 1
    J

    I very simple to understand Java implementation.

        public int longestPalindrome(String s) {
            int longestLength = 0;
            if (s == null) return longestLength;
            Set<Character> characterSet = new HashSet<Character>();
            for (int i = 0; i < s.length(); i++) {
                char character = s.charAt(i);
                if (characterSet.contains(character)) {
                    characterSet.remove(character);
                    longestLength += 2;
                } else {
                    characterSet.add(character);
                }
            }
            return (characterSet.size() > 0) ? longestLength + 1 : s.length();
        }
    

Log in to reply
 

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