您如何检查单词是否有回文的字谜?
您如何将回文词与字谜中新形成的词之一进行比较?
How do you compare a palindromic word to one of the newly formed words of an anagram?
您如何抓取一个新形成的单词以使其与输入单词进行比较?
And how do you grab one of the newly formed words for it to be compared to the input word?
这是我的代码:
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
,因为 MMAAD
的字谜是 madam
.而且我必须检查 madam
是否确实是 mmaad
的回文.
It's not yet done because the permutation and comparison won't work. The output is displaying false
, I need it to be true
because the anagram of MMAAD
is madam
. And I have to check if madam
is indeed a palindrome of mmaad
.
所以我所做的是使用HashMap而不是根据给定的 word
创建 words
字符串的长度可以为偶数
或 odd
长度
So What I did is used HashMap instead of creating words
from the given word
A String can be of even
or odd
length
如果偶数"为字符串是回文,则每个字符"都是回文. String
中的字符会出现 even
次
例如:String str = maam:m = 2,a = 2
If "EVEN" String is palindrome then every "character" in the String
will appear even
times
eg: String str = maam : m=2, a=2
如果"ODD"为字符串是一个回文,那么将只有1个字符出现 odd
,其余的将出现 even
次.
例如:String str = mmaad:m = 2,a = 2,d = 1
If "ODD" String is a palindrome then there will only be 1 character of odd
occurrence and the rest will occur even
times.
eg: String str = mmaad: m=2,a=2,d=1
要在字符串中存储字符的出现,我们将使用HashMap,其中字符串的字符是 KEY
,并且它的出现是 VALUE
To store the Occurrence of the Characters in the String we will use HashMap where Character of the String is the KEY
and its occurrence is VALUE
HashMap<Character,Integer> mapChar = new HashMap<>();
我们将在 HashMap
中添加每个字符以及出现在字符串中的次数.
And we will add each Character in the HashMap
with the number of times it has appeared in the String.
现在,我们将检查字符串长度
是否为偶数".或奇数"如果偶数"为长度字符串,我们知道每个字符都会出现 EVEN
次,如果有时间出现,则出现"ODD".我们返回 false
的时间,即不是回文
Now we will check if the String length
is "even" or "odd"
if "EVEN" Length String we know that every character will appear EVEN
times and if any time a character appears "ODD" times we return false
i.e It's not a Palindrome
for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
if (entries.getValue() % 2 != 0) {
return false;
}
}
如果"ODD"为长度字符串,我们知道只有一个字符会出现 odd
时间,其余字符会出现 EVEN
次
并且如果有2个字符出现 odd
次,则它不是回文
If "ODD" Length String we know that only one Character will appear odd
time and the rest will be of EVEN
Occurrence
And if there are 2 characters that occur odd
times then its not a palindrome
// 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;
}
}
}
这是完整的代码:
Here's the whole Code:
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