Share my Java Solution beats 94.38%


  • 0
    J
    public class Solution {
        public int longestPalindrome(String s) {
            if(s == null || s.length() == 0) {
                return 0;
            }
            
            int single = 0;
            char[] arr = s.toCharArray();
            int[] counts = new int[128];
            for(char c : arr) {
                int num = ++counts[c];
                single++;
                if(num == 2) {
                    counts[c] = 0;
                    single -= 2;
                }
            }
            
            return single == 0 ? arr.length : arr.length - single + 1;
        }
    }

Log in to reply
 

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