Loading
Leetcode / longest-palindrome

Pick a programming language:

Here is the source code for the solution to this problem.

import java.util.HashSet;

class Solution {
    public int longestPalindrome(String s) {
        HashSet<Character> seenCharacters = new HashSet<Character>();
        int count = 0;

        for (int i = 0; i < s.length(); i++)
        {
            if (seenCharacters.contains(s.charAt(i)))
            {
                count += 2;
                seenCharacters.remove(s.charAt(i));
            }
            else
            {
                seenCharacters.add(s.charAt(i));
            }
        }

        return count < s.length() ? count + 1 : count;
    }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: