Longest Palindromic Substring with Transformations

Java Medium 1 views
Back to Questions

Problem Description

Given a string and a maximum number of allowed character transformations, find the longest palindromic substring that can be formed by transforming at most k characters. A transformation means changing any character to any other character.

Input Format

- First line: String s (1 ≤ |s| ≤ 1000) containing lowercase English letters
- Second line: Integer k (0 ≤ k ≤ |s|) representing maximum allowed transformations

Output Format

- Single line: The longest palindromic substring that can be formed
- If multiple palindromes of same maximum length exist, return the lexicographically smallest one
- If no palindrome can be formed, return ""

Sample Test Case

Input:
abccba 1
Output:
abccba

Constraints

- Time complexity: O(n²) or better
- Space complexity: O(n²) or better
- Handle edge cases like single character strings
- Handle cases where k = 0 (must find existing palindrome)

Solutions (0)

No solutions submitted yet. Be the first!

Discussion (0)

No comments yet. Start the discussion!

Prev