Count # of odd but not exact # of appearance Java

  • 0

    Maybe this solution is too simple to be posted by you guys. Anyway, for someone who want to achieve this idea in Java, here it is. I use boolean[] instead of counting # of each character.

    public int longestPalindrome(String s) {
            int n = s.length();
            boolean[] odd = new boolean[60];
            for(char c : s.toCharArray())
                odd[c - 'A'] = !odd[c - 'A'];
            int odds = 0;
            for(boolean b : odd){
                if(b == true)
            return odds == 0 ? n : (n+1-odds);

Log in to reply

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