Is there any simpler solution? (Java)


  • 0
    public class Solution {
        public int longestPalindrome(String s) {
            int[] pool = new int[52];
            int res = 0;
            for(int i = 0; i < s.length(); i++){
                if(s.charAt(i) - 'a' < 0)
                    pool[s.charAt(i) - 'A' + 26] += 1;
                else pool[s.charAt(i) - 'a'] += 1;
            }
            boolean state = false;
            for(int i = 0; i < 51; i++){
                if(pool[i] % 2 == 1){
                    state = true;
                    res += pool[i] - 1;
                }
                else res += pool[i];
            }
            if(state)
                return res + 1;
            else return res;
        }
    }
    

Log in to reply
 

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