HashSet jusst 1 pass


  • 0
    Y
    public int longestPalindrome(String s) {
            Set<Character> set = new HashSet<>();
            int count = 0;
            for (char c : s.toCharArray()) {
                if (!set.add(c)) {
                    set.remove(c);
                    count += 2;
                }
            }
            return set.isEmpty() ? count : count+1;
        }
    

Log in to reply
 

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