BackmediumStrings Cred

Data Center Symmetry Correction Solution

Problem Statement

A data center transmits packets as strings of lowercase English letters. A packet is considered valid if it is a palindrome. Determine if the given string is currently a palindrome, or if it can be transformed into a palindrome by performing exactly one adjacent character swap.

Example 1
Input
racerac
Output
true

Explanation: The string is already a palindrome.

Example 2
Input
abccba
Output
true

Explanation: The string is already a palindrome; no swap is needed.

Example 3
Input
taceca
Output
true

Explanation: Swapping the characters at index 0 and 1 ('t' and 'a') results in 'atceca', which is not a palindrome. However, swapping index 4 and 5 ('c' and 'a') results in 'taceac', which is not. Wait, swapping index 0 and 1 in 'taceca' gives 'atceca'. Actually, 'taceca' can become 'aceca' if we remove one, but the task is a swap. Let's use: 'baabcd'. Swapping index 0 and 1 results in 'ababcd' (no). Swapping index 2 and 3 in 'racecr' (original 'racecr' is not a palindrome). Let's use 'rceacr'. Swapping index 1 and 2 gives 'reaccr', not a palindrome. Actually, 'baab' -> swap index 1 and 2 gives 'abba' (palindrome).

Example 4
Input
abcde
Output
false

Explanation: No single adjacent swap can transform 'abcde' into a palindrome.

Constraints

  • 2 <= s.length <= 10^5
  • s consists of lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free