Java using Hashset: counting the odd pairs


  • 0
    H
    public int longestPalindrome(String s) {
        
        
        HashSet<Character> set = new HashSet<Character>();
        
        int len = 0;
        
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (set.contains(c))  {
                len += 2;
                set.remove(c);
            } else {
                set.add(c);
            }
            
        }
        
        return len  + (set.isEmpty() ? 0 : 1);
        
    }

Log in to reply
 

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