← Google bank

Google · Question 21 · Backtracking

Palindrome Permutation

BacktrackingPythoncode⏱ O(n)💾 O(1)
Product-based

Problem

Check if any permutation of the string can form a palindrome.

Approach

A palindrome can have at most one odd-count character.

Time O(n)Space O(1)

How it works

  1. 1Count frequencies of all characters.
  2. 2Count how many characters have odd counts.
  3. 3Return True if at most one, else False.

Sample input / output

Input:
code
Output:
False

Constraints

1 <= s.length <= 5000; s consists of lowercase English letters.

Solution — read it, then walk it

Time O(n)Space O(1)Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesnoSTARTfrom collections importCounterDEFINEcanPermutePalindrome(s)counts = Counter(s)odd_count = sum(v % 2 for vin counts.values())RETURN odd_count <= 1DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)s = next(it)print(canPermutePalindrome(s))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/14

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits