99% java

  • 0

    You dont need care about how to create the new string, you just count all chars' min even count, and then you +1 for the mid char which counted as odd number, if there is no odd count char, then ignore it.

    public class Solution {
        public int longestPalindrome(String s) {
            if (s=="" || s==null) return 0;
            int map[] = new int[52];
            for (char c: s.toCharArray()){
                if ((int)c>90) ++map[c-'a'];
                else ++map[c-'A'+26];
            int rst = 0;
            boolean odd = false;
            for (int i: map){
                if (i%2==0) rst+=i;
                    rst += (i-1);
                    odd = true;
            if(odd==true) rst++;
            return rst;

Log in to reply

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