BackeasyStringsInfosysHCL

Iterative Pointer Alignment Solution

Problem Statement

Given a string s consisting of lowercase English letters, determine if the string can be transformed into a palindrome by rearranging its characters. A palindrome reads the same forwards and backwards. To solve this, utilize a character frequency map to count the occurrences of each character. The key insight is that a string can form a palindrome if and only if at most one character has an odd frequency count. If more than one character has an odd count, it is impossible to arrange the characters symmetrically around a center. Your task is to return true if the string can be rearranged into a palindrome, and false otherwise.

Example 1
Input
s = "aab"
Output
true

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 1 time (odd). There is only one character with an odd frequency. We can arrange the string as "aba", which is a palindrome. Therefore, return true.

Example 2
Input
s = "abc"
Output
false

Explanation: Count the frequencies: 'a' appears 1 time (odd), 'b' appears 1 time (odd), 'c' appears 1 time (odd). There are three characters with odd frequencies. Since more than one character has an odd count, it is impossible to form a palindrome. Therefore, return false.

Example 3
Input
s = "aabbcc"
Output
true

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 2 times (even), 'c' appears 2 times (even). All characters have even frequencies. We can arrange the string as "abccba" or "abcabc" (wait, abcabc is not a palindrome, but abccba is). Since zero characters have an odd frequency, it is possible to form a palindrome. Therefore, return true.

Example 4
Input
s = "aabbcd"
Output
false

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 2 times (even), 'c' appears 1 time (odd), 'd' appears 1 time (odd). There are two characters with odd frequencies ('c' and 'd'). Since more than one character has an odd count, it is impossible to form a palindrome. Therefore, return false.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters only
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Iterative Pointer Alignment — Problem Statement & Solution Guide

StringsEasyCharacter Frequency Map
TimeO(n)
|
SpaceO(1)

Problem Description

Given a string s consisting of lowercase English letters, determine if the string can be transformed into a palindrome by rearranging its characters. A palindrome reads the same forwards and backwards. To solve this, utilize a character frequency map to count the occurrences of each character. The key insight is that a string can form a palindrome if and only if at most one character has an odd frequency count. If more than one character has an odd count, it is impossible to arrange the characters symmetrically around a center. Your task is to return true if the string can be rearranged into a palindrome, and false otherwise.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Iterative Pointer Alignment"

easy

WHY DOES IT MATTER?

The frequency‑count pattern is fundamental for any problem that requires reasoning about character distribution, such as anagrams, pangrams, and palindrome permutations. Mastering it enables candidates to solve a wide class of string problems efficiently.

OPTIMIZATION CHALLENGE

The key insight is that you don't need the full ordering of characters—only the parity (odd/even) of their counts. By collapsing the problem to a simple odd‑count check, you eliminate the need for sorting or generating permutations, cutting time from factorial to linear.

REAL-WORLD CONNECTION

Think of load balancing in distributed systems: each request type (character) must be paired with a complementary counterpart to achieve symmetry, similar to how services must have matching request/response pairs for a balanced system.

During an interview, first write the frequency counting loop, then immediately compute the odd count in the same pass if possible. This shows you can think in a single‑pass mindset and reduces mental overhead.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The core of this problem lies in the properties of palindromes. A palindrome can have at most one character with an odd frequency because characters must be mirrored around the center; every character on the left half must have a matching counterpart on the right half. Consequently, if more than one character appears an odd number of times, there is no way to arrange the string into a symmetric sequence.

A naive solution might attempt to generate all permutations of the string and check each for palindrome validity, which leads to factorial time complexity and quickly becomes infeasible for strings longer than 10 characters. Instead, the optimal paradigm leverages a frequency map (or an array of size 26 for lowercase English letters) to count occurrences in a single linear pass. By scanning the frequency map a second time, we simply count how many characters have odd frequencies, which determines feasibility in O(n) time.

This approach exemplifies the "counting / frequency" pattern, a staple in string manipulation problems. It reduces both time and space overhead dramatically: the algorithm runs in linear time relative to the input length and uses constant extra space because the alphabet size is fixed. This makes it scalable to very large inputs where brute‑force enumeration would be impossible.

Interview Questions on This Problem

Q1How would you modify the solution if the string could contain Unicode characters beyond lowercase English letters?

Use a hash map (e.g., unordered_map<char, int> in C++ or dict in Python) to store frequencies instead of a fixed-size array, still counting odd occurrences in a second pass. The time remains O(n) while space becomes O(k) where k is the number of distinct characters.

Q2Can you determine in O(1) additional space whether a string can be permuted into a palindrome without storing the full frequency map?

Yes. Maintain a 26‑bit integer as a bitmask where each bit toggles when a character is seen; after processing the string, the mask will have at most one bit set for a valid palindrome permutation. This uses constant space and O(n) time.

Q3Explain how you would extend this algorithm to return one possible palindrome arrangement, not just a boolean answer.

After confirming feasibility, build the palindrome by placing half of each even‑count character on the left side, mirroring it on the right, and inserting the odd‑count character (if any) in the middle. This can be done in O(n) time using a list or string builder.

Examples

Example 1

Input

s = "aab"

Output

true

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 1 time (odd). There is only one character with an odd frequency. We can arrange the string as "aba", which is a palindrome. Therefore, return true.

Example 2

Input

s = "abc"

Output

false

Explanation: Count the frequencies: 'a' appears 1 time (odd), 'b' appears 1 time (odd), 'c' appears 1 time (odd). There are three characters with odd frequencies. Since more than one character has an odd count, it is impossible to form a palindrome. Therefore, return false.

Example 3

Input

s = "aabbcc"

Output

true

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 2 times (even), 'c' appears 2 times (even). All characters have even frequencies. We can arrange the string as "abccba" or "abcabc" (wait, abcabc is not a palindrome, but abccba is). Since zero characters have an odd frequency, it is possible to form a palindrome. Therefore, return true.

Example 4

Input

s = "aabbcd"

Output

false

Explanation: Count the frequencies: 'a' appears 2 times (even), 'b' appears 2 times (even), 'c' appears 1 time (odd), 'd' appears 1 time (odd). There are two characters with odd frequencies ('c' and 'd'). Since more than one character has an odd count, it is impossible to form a palindrome. Therefore, return false.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters only

Optimal Approach & Strategy

Count character frequencies in a single pass, then verify that no more than one character has an odd count, achieving linear time and constant space.

Brute Force Approach

Generate every permutation of the string and test each for palindrome property, which is factorial time and impractical for moderate lengths.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) { return nums.reduce((a, b) => a + b, 0); }

Asked in Top Tech Interviews

InfosysHCL

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.