java - 你如何检查一个单词是否有一个回文的字谜?
问题描述
您如何将回文词与字谜的新形成词之一进行比较?
以及如何获取一个新形成的单词以将其与输入单词进行比较?
这是我的代码:
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
is的字谜madam
。我必须检查是否madam
确实是mmaad
.
解决方案
所以我所做的是使用 HashMap 而不是words
从给定word
的 A String 可以是even
或odd
长度创建
如果“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
推荐阅读
- android - 将 onClick 侦听器添加到 GoogleMaps 容器
- python - 使用窗口数据集进行时间序列预测
- html - 谁能帮我弄清楚为什么我不能将我的粘性顶部导航栏更改为左侧?CSS
- mysql - 更新未对空值执行
- javascript - 在反应中显示按钮列表
- php - onclick 函数调用在 laravel 中未定义
- python - 为什么寻呼机不使用更少的命令打开?
- r - 如何重置 R 中的顺序计数?
- aws-cdk - aws sam local invoke 命令返回错误:找不到模块错误
- reactjs - 覆盖 React Framer Motion 中的过渡延迟