Check Whether Any Rearrangement of a String Can Form a Palindrome

Quick Overview

Decide whether the letters of a lowercase string can be rearranged into a palindrome. The problem tests reasoning about character counts, proving when a rearrangement exists, and writing a linear-time check with constant extra space.

Check Whether Any Rearrangement of a String Can Form a Palindrome

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s`, return `True` if a permutation of the string could form a palindrome and `False` otherwise. ### Function Signature ```python def can_permute_palindrome(s: str) -> bool: ``` ### Constraints - `1 <= len(s) <= 5000` - `s` consists of only lowercase English letters. ### Examples ```text Input: s = "code" Output: False ``` The letters c, o, d and e each appear once, so four letters have an odd count and no arrangement of them reads the same in both directions. ```text Input: s = "aab" Output: True ``` The permutation "aba" is a palindrome. Only b has an odd count (1), and it takes the middle position. ```text Input: s = "carerac" Output: True ``` The letters c, a and r appear twice each and e appears once, so "racecar" is a valid rearrangement.

Overview: Decide whether the letters of a lowercase string can be rearranged into a palindrome. The problem tests reasoning about character counts, proving when a rearrangement exists, and writing a linear-time check with constant extra space.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, return True if a permutation of the string could form a palindrome and False otherwise.

Function Signature

def can_permute_palindrome(s: str) -> bool:

Constraints

  • 1 <= len(s) <= 5000
  • s consists of only lowercase English letters.

Examples

Input: s = "code"
Output: False

The letters c, o, d and e each appear once, so four letters have an odd count and no arrangement of them reads the same in both directions.

Input: s = "aab"
Output: True

The permutation "aba" is a palindrome. Only b has an odd count (1), and it takes the middle position.

Input: s = "carerac"
Output: True

The letters c, a and r appear twice each and e appears once, so "racecar" is a valid rearrangement.