All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0409_longest_palindrome.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0401_0500.s0409_longest_palindrome;

// #Easy #String #Hash_Table #Greedy

import java.util.BitSet;

public class Solution {
    public int longestPalindrome(String s) {
        BitSet set = new BitSet(60);
        for (char c : s.toCharArray()) {
            set.flip(c - 'A');
        }
        if (set.isEmpty()) {
            return s.length();
        }
        return s.length() - set.cardinality() + 1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy