有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使给定字符串成为回文

有人能讨论并解释一下我可以修改代码以适应这些测试用例的方法吗。。。我试图通过替换一个单词中的一个字母来让我的程序接受一个单词,并使其成为回文,从而防止该单词成为回文

所需的测试用例:

Palindromes.isPalindrome2("cat", 'c') => true
Palindromes.isPalindrome2("axaa", 'x') => true
Palindromes.isPalindrome2("12bb", 'b') => true
Palindromes.isPalindrome2("ca", 'c') => true

这就是我到目前为止所做的

public class Palindromes {

    public static boolean isPalindrome(String word) {
        //Strip out non-alphanumeric characters from string
        String cleanWord = word.replaceAll("[^a-zA-Z0-9]","");
        //Check for palindrome quality recursively
        return checkPalindrome(cleanWord);
    }

    public static boolean isPalindrome2(String word) {
        //Strip out non-alphanumeric characters from string
        String cleanWord = word.replaceAll("[^a-zA-Z0-9]","");
        //Check for palindrome quality recursively
        return checkPalindrome2(cleanWord);
    }

    public static boolean checkPalindrome(String word) {
        if(word.length() < 2) { 
            return true;  
        }
        char first  = word.charAt(0);
        char last   = word.charAt(word.length()-1);
        if(first != last) { 
            return false; 
        }
        else { 
            return checkPalindrome(word.substring(1,word.length()-1));
        }
    }

    public void replace(int first, int last) {
        if(first != last)
        { first = last;}
        else if(last != first) 
        { last = first;}
        }

    public static boolean checkPalindrome2(String word) {
        char special = 0;
        if(word.length() < 2) { 
            return true;  
        }
        char first  = word.charAt(0);
        char last   = word.charAt(word.length()-1);
        if(first != last) { 
            return false; 
        }
        if(first != last)
            return false; 
        else {
            return checkPalindrome2(word.substring(1,word.length()-1));
        }
    }
}

replace()是我处理通配符的尝试,但我似乎找不到合适的解决方案。。。我们将非常感谢您的帮助。谢谢


共 (1) 个答案

  1. # 1 楼答案

    以下是我的步骤:

    • 将收到的字符串拆分为2个子字符串。第一个字符串front是字符串的前半部分,第二个字符串back是字符串的半部分

    例如:

    char replacement = 'c';
    
    String input = "aabbcc";
    StringBuilder front = new StringBuilder(input.substring(0, input.length()/2));
    // Do modulus to not include the odd middle (it mirrors itself)
    StringBuilder back = new StringBuilder(input.substring((input.length()/2)+(input.length()%2));
    
    • 比较两个字符串,如果其中一个匹配,则替换另一个。如果两个字符串都不匹配且不是给定的“替换”字符,则返回false。如果你做了不止一次替换,返回false(因为这就是你所说的要求)

    例如:

    int replacements = 0;
    for (int i=0; i < front.length(); ++i)
    {
        int backIndex = back.length() - i;
        if (front.charAt(i) != back.charAt(backIndex))
        {
            // Characters do not match at all to given replacement
            if ((front.charAt(i) != replacement) &&
                (back.charAt(backIndex) != replacement)
            {
                // Cannot make it 
                // (Or if you want to force it, set both to replacement
                //  by deleting this one if statement)
                return false;
            }
            // Front matches replacement
            else if (front.charAt(i) == replacement)
            {
                // Replace back character with replacement
                back.setCharAt(backIndex, replacement);
                replacements++;
            }
            // Back matches replacement
            else if (back.charAt(backIndex) == replacement)
            {
                // Replace front character with replacement
                front.setCharAt(i, replacement);
                replacements++;
            }
            if (replacements > 1)
            {
                // Can only replace one
                return false;
            }
        }
    }
    
    String output = front.toString() + back.toString();