abcde 1
3
import java.util.*;
public class LongestPalindromicSubstring {
public static String longestPalindromeWithKTransformations(String s, int k) {
int n = s.length();
if (n == 0) return "";
if (n == 1) return s;
String result = "";
int maxLength = 0;
// Try every possible center for palindrome
for (int center = 0; center < n; center++) {
// Check for odd length palindromes
String oddPal = expandAroundCenter(s, center, center, k);
if (oddPal.length() > maxLength ||
(oddPal.length() == maxLength && oddPal.compareTo(result) < 0)) {
maxLength = oddPal.length();
result = oddPal;
}
// Check for even length palindromes
String evenPal = expandAroundCenter(s, center, center + 1, k);
if (evenPal.length() > maxLength ||
(evenPal.length() == maxLength && evenPal.compareTo(result) < 0)) {
maxLength = evenPal.length();
result = evenPal;
}
}
return result;
}
private static String expandAroundCenter(String s, int left, int right, int k) {
int n = s.length();
int transformationsNeeded = 0;
int bestLeft = left;
int bestRight = right;
// Expand as much as possible within k transformations
while (left >= 0 && right < n) {
if (s.charAt(left) != s.charAt(right)) {
transformationsNeeded++;
}
if (transformationsNeeded > k) {
break;
}
bestLeft = left;
bestRight = right;
left--;
right++;
}
// Build the palindrome string
StringBuilder palindrome = new StringBuilder();
for (int i = bestLeft; i <= bestRight; i++) {
// Use the character that appears more frequently in the symmetric positions
char leftChar = s.charAt(bestLeft + (bestRight - i));
char rightChar = s.charAt(i);
if (leftChar == rightChar) {
palindrome.append(leftChar);
} else {
// Choose lexicographically smaller character
palindrome.append((leftChar < rightChar) ? leftChar : rightChar);
}
}
return palindrome.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int k = scanner.nextInt();
String result = longestPalindromeWithKTransformations(s, k);
System.out.println(result);
scanner.close();
}
}
import java.util.*;
public class LongestPalindromicSubstring {
public static String longestPalindromeWithKTransformations(String s, int k) {
int n = s.length();
if (n == 0) return "";
if (n == 1) return s;
String result = "";
int maxLength = 0;
// Try every possible center for palindrome
for (int center = 0; center < n; center++) {
// Check for odd length palindromes
String oddPal = expandAroundCenter(s, center, center, k);
if (oddPal.length() > maxLength ||
(oddPal.length() == maxLength && oddPal.compareTo(result) < 0)) {
maxLength = oddPal.length();
result = oddPal;
}
// Check for even length palindromes
String evenPal = expandAroundCenter(s, center, center + 1, k);
if (evenPal.length() > maxLength ||
(evenPal.length() == maxLength && evenPal.compareTo(result) < 0)) {
maxLength = evenPal.length();
result = evenPal;
}
}
return result;
}
private static String expandAroundCenter(String s, int left, int right, int k) {
int n = s.length();
int transformationsNeeded = 0;
int bestLeft = left;
int bestRight = right;
// Expand as much as possible within k transformations
while (left >= 0 && right < n) {
if (s.charAt(left) != s.charAt(right)) {
transformationsNeeded++;
}
if (transformationsNeeded > k) {
break;
}
bestLeft = left;
bestRight = right;
left--;
right++;
}
// Build the palindrome string
StringBuilder palindrome = new StringBuilder();
for (int i = bestLeft; i <= bestRight; i++) {
// Use the character that appears more frequently in the symmetric positions
char leftChar = s.charAt(bestLeft + (bestRight - i));
char rightChar = s.charAt(i);
if (leftChar == rightChar) {
palindrome.append(leftChar);
} else {
// Choose lexicographically smaller character
palindrome.append((leftChar < rightChar) ? leftChar : rightChar);
}
}
return palindrome.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int k = scanner.nextInt();
String result = longestPalindromeWithKTransformations(s, k);
System.out.println(result);
scanner.close();
}
}