Reputation: 109
Here i have code to check whether given string is palindrome or not but don't know the procedure to reduce characters from last.To make change the character i need to follow the rule as: Eg String input="abcde"
.Letter 'e' can be converted to 'd' but not 'e' to 'd' if character becomes 'a' we can't change further.Here is my code:
public static void main(String[] args) {
String input = "abcd";
String temp = input;
String output = "";
for (int i = output.length() - 1; i >= 0; i--) {
output = output + output.charAt(i);
}
if(temp.equals(output)){
System.out.println("String is palindrome");
}else{
System.out.println("Not a palindrome");
}
}
output order logic is as follows:
if input is =abcd
abcc('d' converted to 'c')and check for palindrome
abcb('c' converted to 'b')and check for palindrome
abca('b' converted to 'a')and check for palindrome
abba('a' further we can't change so shift to previous letter 'c' and change to 'b')
and check for palindrome
String is palindrome and count is=4
if input=cbdf
cbde('f' converted to 'f')and check for palindrome
cdbd('e' converted to 'd')and check for palindrome
cdbc('d' converted to 'c')and check for palindrome
cdbb('c' converted to 'b')and check for palindrome
cdba('b' converted to 'a')and check for palindrome
cdaa('a' further we can't change so shift to previous letter 'b' and change to 'a')
and check for palindrome
ccaa('a' further we can't change so shift to previous letter 'd' and change to 'c')
and check for palindrome
cbaa('c' converted to 'b')and check for palindrome
caaa('a' further we can't change so shift to previous letter 'c' and change to 'b')
and check for palindrome
baaa('b' converted to 'a')and check for palindrome
aaaa now string is palindrome
String is palindrome and count is=10
finally i need the count to make string palindrome.
Upvotes: 0
Views: 2596
Reputation: 4129
Assuming that I understand your problem, you are going about this in an inefficent way. Here is how I would do this:
String toPalindrome(String str){
StringBuilder reverse = new StringBuilder(str).reverse();
for(int idx = 0; idx < str.size()/2; idx++)
if(str.getCharAt(idx) < reverse.getCharAt(idx))
reverse.setCharAt(idx, str.getCharAt(idx));
return reverse.subString(0,str.size()/2) + reverse.reverse().subString(str.size()/2);
}
Aside from the off-by-one errors in this code, this should work to produce the output you need.
Using this approach, we don't have to decrement each character one by one - we rather just immediately replace each character with the target value. We also never have to check if it is a palindrome, since this method is guaranteed to produce one (after all, it is concatenating a string with its mirror image in the return step).
EDIT: seeing that we need only return the number of times characters were decremented, we can do something even simpler:
int abs(int x){
return x>0?x:-x;
}
int palindromeCounts(String str){
int count = 0;
for(int idx = 0; idx < str.length()/2; idx++)
count += abs(str.charAt(idx) - reverse.charAt(str.length()-1-idx));
return count;
}
Upvotes: 1