266.Palindrome-Permutation

266. Palindrome Permutation

题目地址

https://leetcode.com/problems/palindrome-permutation/

题目描述

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

代码

Approach #1 Brute Force

If a string with an even length is a palindrome, every character in the string must always occur an even number of times. If the string with an odd length is a palindrome, every character except one of the characters must always occur an even number of times. Thus, in case of a palindrome, the number of characters with odd number of occurences can't exceed 1(1 in case of odd length and 0 in case of even length).

Approach #2 Using HashMap

Approach #3 Using Array

Approach #4 Single Pass

Approach #5 Using Set

Last updated

Was this helpful?