资讯专栏INFORMATION COLUMN

[LeetCode] 266. Palindrome Permutation

jlanglang / 1289人阅读

Problem

Given a string, determine if a permutation of the string could form a palindrome.

Example 1:

Input: "code"
Output: false
Example 2:

Input: "aab"
Output: true
Example 3:

Input: "carerac"
Output: true

Solution
class Solution {
    public boolean canPermutePalindrome(String s) {
        Set set = new HashSet<>();
        for (char ch: s.toCharArray()) {
            if (set.contains(ch)) set.remove(ch);
            else set.add(ch);
        }
        return set.size() <= 1;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72066.html

相关文章

  • [Leetcode] Palindrome Permutation 回文变换

    摘要:最笨的方法就是用的解法,找出所有的,然后再用中判断回文的方法来判断结果中是否有回文。而中心对称点如果是字符,该字符会是奇数次,如果在两个字符之间,则所有字符都是出现偶数次。 Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. F...

    svtter 评论0 收藏0
  • LC 267 Palindrome Permutation II

    摘要:判断一个能否组成一个第一次出现增加一,第二次出现减少一。出现偶数次的最终对被抵消。出现基数词的则会让加一。类似于,奇数个的那个单独考虑,必须放中间。得到各个字符一半数量的长度数字的终止条件,利用的对称性输出结果。 Given a string s, return all the palindromic permutations (without duplicates) of it. R...

    lowett 评论0 收藏0
  • [LeetCode] 267. Palindrome Permutation II

    Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...

    huashiou 评论0 收藏0
  • leetcode 部分解答索引(持续更新~)

    摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...

    leo108 评论0 收藏0
  • LeetCode 精选TOP面试题【51 ~ 100】

    摘要:有效三角形的个数双指针最暴力的方法应该是三重循环枚举三个数字。总结本题和三数之和很像,都是三个数加和为某一个值。所以我们可以使用归并排序来解决这个问题。注意因为归并排序需要递归,所以空间复杂度为 ...

    Clect 评论0 收藏0

发表评论

0条评论

jlanglang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<