Java-HashSet Solution very easy to understand


  • 1
    S

    public class Solution {
    public int longestPalindrome(String s) {
    if(s == null)
    return 0;
    HashSet<Character> set = new HashSet<Character>();

        for(int i = 0; i < s.length(); i++) {
            if(set.contains(s.charAt(i))) {
                set.remove(s.charAt(i));
            } else {
                set.add(s.charAt(i));
            }
        }
        if(set.size()>0)
        return s.length()-set.size()+1;
        else
        return s.length()-set.size();
    }
    

    }


Log in to reply
 

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