Very Easy Java Solution O(n) Single Pass runs in very less time


  • 0
    K
        
        public int longestPalindrome(String s) {
            if (s == null) {
                throw new IllegalArgumentException("Invalid Input");
            }
            int l = s.length();
            if (l == 0) {
                return 0;
            }
            int[] count = new int[256];
            for (int i = 0; i < l; i++) {
                count[s.charAt(i)]++;
            }
            int oddLength = 0;
            int length = 0;
            for (int currCount : count) {
                if (currCount % 2 == 0) {
                    length += currCount;
                }
                else {
                    oddLength = Math.max(oddLength, currCount);
                }
            }
            length += oddLength;
            return length;
        }
    }'''

Log in to reply
 

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