9ms without Hash Java, but can you help me to improve my solution? TY


  • 0
    A
    public class Solution {
        public int longestPalindrome(String s) {
            if (s == null || s.length() == 0) {
                return 0;
            }
            char[] array = s.toCharArray();
            int[] tmp = new int[52];
            boolean flag = true;
            int res = 0;
            for (int i = 0; i < array.length; i ++) {
                if (array[i] > 'Z') {
                    tmp[array[i] - 'a' + 26] ++;
                } else {
                    tmp[array[i] - 'A'] ++;
                }
            }
            for (int i = 0; i < tmp.length; i ++) {
                if (tmp[i] % 2 != 0) {
                    flag = false;
                }
                res = res + tmp[i] / 2 * 2;
            }
            return flag == true ? res : res + 1;
        }
    }
    

Log in to reply
 

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