Java + No HashSet + One Pass + 7 main lines


  • 0
    public class Solution {
        public int longestPalindrome(String s) {
           int count[] = new int[128];
           int odd = 0, even = 0;
           for(int i = 0; i < s.length(); i++){
               if(++count[s.charAt(i) - 'A'] % 2 == 0){
                    even ++; odd --;
               }
               else odd ++;
           }
           return even * 2 + (odd > 0 ? 1 : 0);
        }
    }
    

Log in to reply
 

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