首页 > 解决方案 > 你如何检查一个单词是否有一个回文的字谜?

问题描述

您如何将回文词与字谜的新形成词之一进行比较?

以及如何获取一个新形成的单词以将其与输入单词进行比较?

这是我的代码:

public class SampleCode2 {
    public static boolean isPalindromic(String word, String mark) {
        if (word.length() == 0) {
        }

        for (int i = 0; i < word.length(); i++) {
            String newMark = mark + word.charAt(i);
            String newLetters = word.substring(0, i) +
                    word.substring(i + 1);
        }

        String ifPalindrome = ""; //will store here the reversed string
        String original = word; //will store here the original input word

        //to reverse the string
        for (int i = word.length() - 1; i >= 0; i--) {
            ifPalindrome += word.charAt(i);
        }

        //to compare the reversed string to the anagram
        if (word.equals(ifPalindrome)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        boolean check = isPalindromic("mmaad", "");
        System.out.println(check);
    }
}

它还没有完成,因为排列和比较不起作用。输出正在显示false,我需要它是true因为MMAADis的字谜madam。我必须检查是否madam确实是mmaad.

标签: javastringwordpalindromeanagram

解决方案


所以我所做的是使用 HashMap 而不是words从给定word
的 A String 可以是evenodd长度创建


如果“EVEN”字符串是回文,那么其中的每个“字符”String都会出现even次数,
例如: String str = maam : m=2, a=2


如果“ODD”字符串是回文,那么只会出现 1 个字符,odd其余字符将出现even多次。
例如:字符串 str = mmaad:m=2,a=2,d=1


为了在字符串中存储字符的出现,我们将使用 HashMap,其中字符串的字符是 the KEY,它的出现是VALUE

HashMap<Character,Integer> mapChar = new HashMap<>();

我们将在字符串中添加每个字符以及HashMap它在字符串中出现的次数。

现在我们将检查是否String length是“偶数”或“奇数”如果“偶数”长度字符串我们知道每个字符都会出现EVEN多次,如果任何时候一个字符出现“奇数”次我们返回false即它不是回文

for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
    if (entries.getValue() % 2 != 0) {
        return false;
    }
}

如果“ODD”长度字符串,我们知道只有一个字符会出现odd时间,其余的将是EVEN出现次数
如果有 2 个字符出现odd次数,那么它不是回文

// Number of times odd value Character as occurred
int occur1 = 0;
for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
    if (entries.getValue() % 2 == 1) {
        occur1++;
        if (occur1 > 1) {
            return false;
        }
    }
}

这是整个代码:

public static void main(String[] args) throws Exception {
    boolean check = isPalindromic("malayalam", "");
    System.out.println(check);
}
public static boolean isPalindromic(String word, String mark) {
    boolean isPal = true;
    if (word.length() == 0) {
        return false;
    }
    HashMap<Character, Integer> mapChar = new HashMap<>();
    for (int i = 0; i < word.length(); i++) {
        char ch = word.charAt(i);
        if (mapChar.containsKey(ch)) {
            mapChar.put(ch, mapChar.get(ch) + 1);
        } else {
            mapChar.put(ch, 1);
        }
    }
    if (word.length() % 2 == 0) {
        for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
            if (entries.getValue() % 2 != 0) {
                return false;
            }
        }
    } else {
        int occur1 = 0;
        for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
            if (entries.getValue() % 2 == 1) {
                occur1++;
                if (occur1 > 1) {
                    isPal = false;
                    break;
                }
            }
        }
    }
    return isPal;
}

输出:

mmaa
Is Palindrome: true
mmaad
Is Palindrome: true
niti
Is Palindrome: false

推荐阅读