C# - HashSet (same as others)


  • 0
    public int LongestPalindrome(string s) {
        HashSet<char> set = new HashSet<char>();
        foreach (char c in s)
        {
            if (set.Contains(c))
            {
                set.Remove(c);
            }
            else
            {
                set.Add(c);
            }
        }
        
        return s.Length - set.Count() + (set.Count() > 0 ? 1 : 0);
    }

Log in to reply
 

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