KatInterview

Validate Palindrome Rearrangements and Sum High-Precision Decimals

StringsModel answer
MediumModel answerStringsstringsmath-bitshash-table

Last updated

Check if a string's characters can form a palindrome, and implement precise addition of non-negative decimal strings without using native floating-point types.

This practice problem consists of two tasks focused on string processing and custom arithmetic.

Part 1: Palindromic Permutation Check

Given a string s, determine whether any arrangement of its characters can form a valid palindrome.

  • Return True if a palindromic permutation exists, otherwise False.
  • The input string may contain any standard printable characters. Case matters (e.g., 'A' and 'a' are distinct).

Part 2: High-Precision Decimal Addition

Given two strings num1 and num2 representing non-negative real numbers in decimal format, return their sum as a string.

  • You must not convert the inputs directly to native floating-point types (like Python's float), nor use built-in arbitrary-precision arithmetic libraries like decimal.Decimal.
  • Inputs may or may not contain a decimal point '.'.
  • Handle edge cases where inputs lack leading integer digits (e.g., ".5"), lack explicit decimal points (e.g., "12"), or end with a trailing decimal point (e.g., "7.").
  • The output must be formatted canonically:
    • No extraneous leading zeros in the integer component (e.g., "07" should become "7", but "0" remains "0").
    • No trailing zeros in the fractional component, and no isolated trailing decimal point (e.g., "1.50" becomes "1.5", and "1.00" becomes "1").

Hints

Hint 1

For Part 1: Consider the structural definition of a palindrome. What constraint does symmetry place on character frequencies?

Hint 2

For Part 2: Try aligning both numbers around their decimal points before adding. How can padding simplify carrying across the boundary?

Hint 3

Pay close attention to stripping extra zeros from the final result while preserving at least one digit for zero values.