My simple easy understand Java AC solution with int[]


  • 0
    Z
    public int longestPalindrome(String s) {
             int[] map = new int['z' - 'A' + 1];
             int result = 0;
             boolean flag = false;
             for(int i = 0; i < s.length(); i++){
                 map[s.charAt(i) - 'A']++;
             }
             for(int j = 0; j < map.length; j++){
                 if(map[j] == 0) continue;
                 if(map[j] % 2 == 0) result += map[j];
                 else{
                     result += map[j] - 1;
                     flag = true;
                 }
             }
             if(flag == true) result += 1;
             return result;
         } 
    

Log in to reply
 

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