Java int[26] solution


  • 0
    J
        public int longestPalindrome(String s) {
            int[][] cs = new int[2][26];
            
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c >= 'a' && c <= 'z') cs[0][c - 'a']++;
                if (c >= 'A' && c <= 'Z') cs[1][c - 'A']++;
            }
            boolean hasOdd = false;
            int maxCnt = 0;
            for (int[] css : cs) {
                for (int cnt : css) {
                    if (cnt % 2 == 0) maxCnt += cnt;
                    else {
                        maxCnt += cnt - 1;
                        hasOdd = true;
                    }
                }
            }
            if (hasOdd) maxCnt++;
            return maxCnt;
        }
    

Log in to reply
 

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