PalindromeFactory
SRM 439 · 2009-04-30 · by it4.kp
SRM 439 · 2009-04-30 · by it4.kp · Dynamic Programming, Simple Search, Iteration
Problem Statement
Problem Statement
A palindrome is a string that reads the same forward and backward. Not all strings are palindromes and we are going to fix that.
In this problem we consider four edit operations:
You can apply the first three operations any number of times, but the last operation can be applied at most once.
You are given aString initial. Return the minimal number of operations needed to make a palindrome from it.
In this problem we consider four edit operations:
- Insert a character at any position of a string (including the beginning and the end)
- Delete a character at any position of a string
- Change a character at any position of a string
- Swap any two different characters (not necessarily adjacent)
You can apply the first three operations any number of times, but the last operation can be applied at most once.
You are given a
Constraints
- initial will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
Examples
0)
"abba" Returns: 0
The string is already a palindrome.
1)
"dabba" Returns: 1
We can delete the first letter to make a palindrome.
2)
"babacvabba" Returns: 2
Delete 'v' and swap the first two characters.
3)
"abc" Returns: 1
We can change 'c' to 'a' to get a palindrome in one operation.
4)
"acxcba" Returns: 1
Insert 'b' after the first character.
5)
"abcacbd" Returns: 1
Swap 'd' with 'a' in the middle.
Submissions are judged against all 51 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class PalindromeFactory with a public method int getEditDistance(string initial) · 51 test cases · 2 s / 256 MB per case