简体   繁体   English

如何在Java中将给定字符串中的字符减少到回文

[英]How to reduce character from given String until it becomes to palindrome in java

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: 在这里,我有代码来检查给定的字符串是否是回文,但是不知道从最后开始减少字符的过程。要更改字符,我需要遵循以下规则:例如: String input="abcde" .Letter'e如果字符变为'a',可以将'转换为'd',但不能将'e'转换为'd',这是我的代码:

 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.

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM