Simple by Java 8 Stream API


  • 0
    X
    public int longestPalindrome(String s) {
        final int[] occurs = new int[128];
        
        int numOfPair = s.chars().reduce(0, (r, ch) -> {
            if(occurs[ch] == 0) {
                occurs[ch]++;
                return r;
            } else {
                occurs[ch]--;
                return r + 1;
            }
        });
        
        return numOfPair == s.length() / 2 ? s.length() : numOfPair * 2 + 1;
    }
    

Log in to reply
 

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