Accepted Java O(n) Solution Using HashSet, Only one scan


  • 1
    Y
    public class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null || s.length() == 0) return true;
        
        HashSet<Character> set = new HashSet<Character>(s.length());
        
        for (int i = 0 ; i < s.length() ; i++) {
            char c = s.charAt(i);
            
            if (set.contains(c)) {
                set.remove(c);
            } else {
                set.add(c);
            }
        }
        
        return set.size() <= 1;
    }
    

    }


Log in to reply
 

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