266. Palindrome Permutation - Easy

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

扫一遍s,统计频率,奇数频率最多只能出现一次。用cnt表示结果,cnt += map[i]的频率%2,如果cnt > 1,则不可能是回文

time: O(n), space: O(n)

class Solution {
    public boolean canPermutePalindrome(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        for(int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }

        int cnt = 0;
        for(Map.Entry<Character, Integer> entry : map.entrySet()) {
            cnt += entry.getValue() % 2;
        }
        return cnt <= 1;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10179670.html

时间: 2024-11-09 02:03:02

266. Palindrome Permutation - Easy的相关文章

266. Palindrome Permutation

/* *266. Palindrome Permutation *2016-6-24 by Mingyang *这个题目很简单的用Hashmap来计算,而高手用的是HashSet来做,遇到一样的,就remove,从没出现的就Add *不过自己的代码还借鉴了如何loop hashmap的value */ public boolean canPermutePalindrome(String s) { int len=s.length(); if(s==null||len==0) return tru

[LeetCode#266] Palindrome Permutation

Problem: Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. General Analysis: This problem is easy. Basic idea is: iff s w

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

[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. For example: Given s = "aabb", return ["abba", "baab"]. Given s

125. Valid Palindrome【easy】

125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. No

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even

[LeetCode] Palindrome Permutation I &amp; II

Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even le

267. Palindrome Permutation II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

Leetcode Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d