JAVA - 10s - Pretty straight forward


  • 1
    N

    '''
    public class Solution {
    public int longestPalindrome(String s) {

        if (s == null || s.length() == 0) return 0;
        if (s.length() == 1) return 1;
        
        int result = 0;
        int[] counter = new int[128];
        
        for (char c : s.toCharArray()) {
            if(counter[c] == 0) {
                counter[c] = 1;
            } else {
                counter[c] = 0;
                result += 2;
            }
        }
    
        return Math.min(result + 1, s.length());
    }
    

    }
    '''


Log in to reply
 

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