你如何检查一个单词是否有一个回文字谜?
How do you check if a word has an anagram that is a palindrome?
你如何比较回文词和变位词中新形成的词之一?
如何抓取一个新形成的词与输入词进行比较?
这是我的代码:
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
.
的回文
所以我所做的是使用 HashMap 而不是从给定的 word
创建 words
字符串可以是 even
或 odd
长度
如果“EVEN”字符串是回文,那么 String
中的每个“字符”将出现 even
次
例如:String str = maam : m=2, a= 2
如果“ODD”字符串是回文,那么只有 1 个字符出现 odd
次,其余字符将出现 even
次。
例如:String str = mmaad: m=2,a=2,d=1
为了存储字符串中字符的出现,我们将使用 HashMap,其中字符串的字符是 KEY
,它的出现是 VALUE
HashMap<Character,Integer> mapChar = new HashMap<>();
然后我们将 HashMap
中的每个字符加上它在字符串中出现的次数。
现在我们将检查 String length
是“偶数”还是“奇数”
如果“EVEN”长度字符串我们知道每个字符都会出现 EVEN
次,如果任何时候一个字符出现“奇数”次我们 return 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
如果您想要查找 回文 单词的 字谜 列表,您可以将此任务分为两部分:首先得到那个单词List<String>
的字符排列列表,即一个字谜列表,然后过滤掉那些是回文的字符串。例如,对于mmaad
字符串,回文为:
madam
amdma
对于大字符串,获取排列列表是一项昂贵的操作,因为排列的数量是 字符串长度的阶乘。例如,对于 mmaad
字符串,有 120 个排列。之后过滤回文更便宜。
public static void main(String[] args) {
// list of distinct permutations
getPermutations("mmaad")
// Stream<String>
.stream()
// filter palindromes
.filter(str -> isPalindrome(str))
// output
.forEach(System.out::println);
}
/**
* @param str source string, may contain surrogate pairs.
* @return whether the source string is a palindrome.
*/
public static boolean isPalindrome(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream
// iterate from the beginning to the middle of the string
.range(0, chars.length / 2)
// compare the characters: first - last, second - penultimate
.allMatch(i -> chars[i] == chars[chars.length - i - 1]);
}
/**
* @param str source string, may contain surrogate pairs.
* @return a list of distinct permutations of characters of the source string.
*/
public static List<String> getPermutations(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream.range(0, chars.length)
// Stream<List<Map<Integer,String>>>
.mapToObj(i -> IntStream.range(0, chars.length)
// represent each character as Map<Integer,String>
.mapToObj(j -> Map.of(j, Character.toString(chars[j])))
// collect a list of maps
.collect(Collectors.toList()))
// reduce a stream of lists to a single list
.reduce((list1, list2) -> list1.stream()
// summation of pairs of elements,
// i.e. maps, from two lists
.flatMap(map1 -> list2.stream()
// filter out those keys
// that are already present
.filter(map2 -> map2.keySet().stream()
.noneMatch(map1::containsKey))
// join entries of two maps
.map(map2 -> {
Map<Integer, String> map =
new LinkedHashMap<>();
map.putAll(map1);
map.putAll(map2);
return map;
}))
// collect into a single list
.collect(Collectors.toList()))
// List<Map<Integer,String>>
.orElse(List.of(Map.of(0, str)))
// Stream<Map<Integer,String>>
.stream()
// map of strings to a single string
.map(map -> String.join("", map.values()))
.distinct()
// list of distinct permutations
.collect(Collectors.toList());
}
另请参阅:
• How to create all permutations of tuples without mixing them?
• Reverse string printing method
你如何比较回文词和变位词中新形成的词之一?
如何抓取一个新形成的词与输入词进行比较?
这是我的代码:
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
.
所以我所做的是使用 HashMap 而不是从给定的 word
创建 words
字符串可以是 even
或 odd
长度
如果“EVEN”字符串是回文,那么 String
中的每个“字符”将出现 even
次
例如:String str = maam : m=2, a= 2
如果“ODD”字符串是回文,那么只有 1 个字符出现 odd
次,其余字符将出现 even
次。
例如:String str = mmaad: m=2,a=2,d=1
为了存储字符串中字符的出现,我们将使用 HashMap,其中字符串的字符是 KEY
,它的出现是 VALUE
HashMap<Character,Integer> mapChar = new HashMap<>();
然后我们将 HashMap
中的每个字符加上它在字符串中出现的次数。
现在我们将检查 String length
是“偶数”还是“奇数”
如果“EVEN”长度字符串我们知道每个字符都会出现 EVEN
次,如果任何时候一个字符出现“奇数”次我们 return 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
如果您想要查找 回文 单词的 字谜 列表,您可以将此任务分为两部分:首先得到那个单词List<String>
的字符排列列表,即一个字谜列表,然后过滤掉那些是回文的字符串。例如,对于mmaad
字符串,回文为:
madam
amdma
对于大字符串,获取排列列表是一项昂贵的操作,因为排列的数量是 字符串长度的阶乘。例如,对于 mmaad
字符串,有 120 个排列。之后过滤回文更便宜。
public static void main(String[] args) {
// list of distinct permutations
getPermutations("mmaad")
// Stream<String>
.stream()
// filter palindromes
.filter(str -> isPalindrome(str))
// output
.forEach(System.out::println);
}
/**
* @param str source string, may contain surrogate pairs.
* @return whether the source string is a palindrome.
*/
public static boolean isPalindrome(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream
// iterate from the beginning to the middle of the string
.range(0, chars.length / 2)
// compare the characters: first - last, second - penultimate
.allMatch(i -> chars[i] == chars[chars.length - i - 1]);
}
/**
* @param str source string, may contain surrogate pairs.
* @return a list of distinct permutations of characters of the source string.
*/
public static List<String> getPermutations(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream.range(0, chars.length)
// Stream<List<Map<Integer,String>>>
.mapToObj(i -> IntStream.range(0, chars.length)
// represent each character as Map<Integer,String>
.mapToObj(j -> Map.of(j, Character.toString(chars[j])))
// collect a list of maps
.collect(Collectors.toList()))
// reduce a stream of lists to a single list
.reduce((list1, list2) -> list1.stream()
// summation of pairs of elements,
// i.e. maps, from two lists
.flatMap(map1 -> list2.stream()
// filter out those keys
// that are already present
.filter(map2 -> map2.keySet().stream()
.noneMatch(map1::containsKey))
// join entries of two maps
.map(map2 -> {
Map<Integer, String> map =
new LinkedHashMap<>();
map.putAll(map1);
map.putAll(map2);
return map;
}))
// collect into a single list
.collect(Collectors.toList()))
// List<Map<Integer,String>>
.orElse(List.of(Map.of(0, str)))
// Stream<Map<Integer,String>>
.stream()
// map of strings to a single string
.map(map -> String.join("", map.values()))
.distinct()
// list of distinct permutations
.collect(Collectors.toList());
}
另请参阅:
• How to create all permutations of tuples without mixing them?
• Reverse string printing method