πŸ“Œ Palindrome Permutation β€” Notes

Read Time:1 Minute, 16 Second

A palindrome permutation checks whether a string’s characters can be rearranged to form a palindrome.

Palindrome Rules

  • A palindrome reads the same forward and backward
  • Characters must appear in pairs
  • At most one character is allowed to appear an odd number of times
String LengthAllowed Odd Count
Even0
Odd1

Steps to Check

  1. Remove spaces
  2. (Optional) Convert to lowercase
  3. Count frequency of each character
  4. Count characters with odd frequency
  5. If oddCount ≀ 1 β†’ βœ… Palindrome permutation possible

Example: "tact coa"

Step 1: Remove space

tactcoa

CharacterCount
t2
a2
c2
o1

Step 3: Odd count = 1 (o)

Step 4:

oddCount ≀ 1 β†’ TRUE

Palindrome permutation exists

Possible Palindrome

taco cat

Interview One-Liner

β€œA string can form a palindrome permutation if no more than one character has an odd frequency.”

Time & Space Complexity

  • Time: O(n)
  • Space: O(1) (fixed alphabet)

Go Program: Palindrome Permutation

package main

import (
	"fmt"
)

func isPalindromePermutation(s string) bool {
	freq := make(map[rune]int)

	// Count characters (ignore spaces)
	for _, ch := range s {
		if ch != ' ' {
			freq[ch]++
		}
	}

	oddCount := 0

	// Check odd frequencies
	for _, count := range freq {
		if count%2 != 0 {
			oddCount++
		}
	}

	return oddCount <= 1
}

func main() {
	str := "tact coa"

	if isPalindromePermutation(str) {
		fmt.Println("Palindrome permutation possible")
	} else {
		fmt.Println("Palindrome permutation NOT possible")
	}
}

How it works (quick)

  1. Ignore spaces
  2. Count frequency of each character
  3. Count how many have odd frequency
  4. Return true if oddCount ≀ 1

Complexity

  • Time: O(n)
  • Space: O(n) (map for characters)

β€œI’m counting character frequencies and ensuring no more than one character has an odd count.”

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %